为了账号安全,请及时绑定邮箱和手机立即绑定

过滤数组旁边的对象

过滤数组旁边的对象

慕雪6442864 2021-11-18 20:28:24
我需要删除未通过的整个对象这是数组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))});
查看完整描述

2 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

您可以通过数组进行映射,然后复制特定项目的所有属性,并分别对列表属性进行过滤。


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) )


查看完整回答
反对 回复 2021-11-18
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

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)

});


查看完整回答
反对 回复 2021-11-18
  • 2 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信