猿问

过滤数组旁边的对象

我需要删除未通过的整个对象


这是数组


const array = [{

    course: 1,

    list: [{

        id: 1,

        name: "john",

        code: true

      },

      {

        id: 1,

        name: "maria",

        code: true

      },

    ]

  },

  {

    course: 2,

    list: [{

        id: 3,

        name: "rose"

      },

      {

        id: 4,

        name: "mark",

        code: true

      }

    ]

  }

]

我需要的是删除没有代码的 obj:true,然后得到这个


const array = [{

    course: 1,

    list: [{

      id: 1,

      name: "john",

      code: true

    }, ]

  },

  {

    course: 2,

    list: [{

      id: 1,

      name: "mark",

      code: true

    }]

  }

]


我试图在过滤器中制作地图,但它根本不起作用


const remove = array.filter(function(lines) {

  return lines.map(line => line.list.map(list => list.code))

});


慕雪6442864
浏览 111回答 2
2回答

白板的微信

您可以通过数组进行映射,然后复制特定项目的所有属性,并分别对列表属性进行过滤。const array = [{    course: 1,    list: [{        id: 1,        name: "john",        code: true      },      {        id: 1,        name: "maria",        code: true      },    ]  },  {    course: 2,    list: [{        id: 3,        name: "rose"      },      {        id: 4,        name: "mark",        code: true      }    ]  }]const filter = arr => arr.map(arrItem => ({      ...arrItem,    list: arrItem.list.filter( listItem => listItem.code )  }))console.log( filter(array) )

凤凰求蛊

const filtered = [];arr.forEach(item => {  const list = item.list.filter(listItem => listItem.code);  if(list.length > 0) {     filter.push({ ...item, list });  }});如果在过滤掉带有code: false. 无论如何要包括它们,你可以这样做:const filtered = arr.map(item => ({  ...item,  list: item.list.filter(listItem => listItem.code)});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答