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

根据另一个嵌套对象数组的内容过滤对象数组

根据另一个嵌套对象数组的内容过滤对象数组

倚天杖 2021-04-16 09:15:18
我想过滤positions数组并删除数组中表示的所有位置people。我尝试了_.forEach和的几种组合,_.filter但似乎无法弄清楚。console.log(position)var test = _.filter(position, function(pos) {    _.forEach(people, function(peo) {        _.forEach(peo.position, function(peoplePos) {            if(peoplePos.value == pos.value){                return false;            }        });    });});console.log(test)我认为,我的主要问题是职位嵌套在每个人的对象中var positions = [{    val: 'CEO',    label: 'CEO XXX'}, {    val: 'CTO',    label: 'CTO XXX'}, {    val: 'CBO',    label: 'CBO XXX'}, {    val: 'CLO',    label: 'CLO XXX'}]var people = [{    id: 'AAA',    positions: [{        val: 'CEO',        label: 'CEO XXX'    }]},{    id: 'BBB',    positions: [{        val: 'CXO',        label: 'CXO XXX'    },{        val: 'CEO',        label: 'CEO XXX'    }]},{    id: 'CCC',    positions: [{        val: 'CTO',        label: 'CTO XXX'    }]}]在这种情况下,我的目标是以下结果:var positions = [{    val: 'CBO',    label: 'CBO XXX'}, {    val: 'CLO',    label: 'CLO XXX'}]由于CBO和CLO没有由people数组中的任何对象表示。
查看完整描述

3 回答

?
慕容3067478

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

一种快速的方法是同时对people数组进行字符串化并检查字符串中的位置。


这免除了您遍历嵌套结构的麻烦。


var positions = [{ val: 'CEO', label: 'CEO XXX' }, { val: 'CTO', label: 'CTO XXX' }, { val: 'CBO', label: 'CBO XXX' }, { val: 'CLO', label: 'CLO XXX' }]


var people = [{ id: 'AAA', positions: [{ val: 'CEO', label: 'CEO XXX' }] }, { id: 'BBB', positions: [{ val: 'CXO', label: 'CXO XXX' }, { val: 'CEO',

    label: 'CEO XXX' }] }, { id: 'CCC', positions: [{ val: 'CTO', label: 'CTO XXX' }] }];


var stringifiedPeople = JSON.stringify(people)


var newPositions = positions.filter((position) =>

  !stringifiedPeople.includes(JSON.stringify(position))

);


console.log(newPositions)

或者,您可以创建一个包含所有占用位置的地图,并过滤掉可用的位置。


var positions = [{ val: 'CEO', label: 'CEO XXX' }, { val: 'CTO', label: 'CTO XXX' }, { val: 'CBO', label: 'CBO XXX' }, { val: 'CLO', label: 'CLO XXX' }]


var people = [{ id: 'AAA', positions: [{ val: 'CEO', label: 'CEO XXX' }] }, { id: 'BBB', positions: [{ val: 'CXO', label: 'CXO XXX' }, { val: 'CEO',

    label: 'CEO XXX' }] }, { id: 'CCC', positions: [{ val: 'CTO', label: 'CTO XXX' }] }];


var mappedPositions = {}


people.forEach((p) =>

  p.positions.forEach((position) =>

    mappedPositions[position.val] = true

  )

);


var newPositions = positions.filter((position) => !mappedPositions[position.val]);


console.log(newPositions)


查看完整回答
反对 回复 2021-04-29
?
PIPIONE

TA贡献1829条经验 获得超9个赞

执行我的评论。


为了.reduce()提高效率,可以将整个事情写成一个很大的数组,但是我更喜欢显示确切的步骤,以使每个步骤的工作更加清晰。


var positions = [{val:'CEO',label:'CEOXXX'},{val:'CTO',label:'CTOXXX'},{val:'CBO',label:'CBOXXX'},{val:'CLO',label:'CLOXXX'}];


var people = [{id:'AAA',positions:[{val:'CEO',label:'CEOXXX'}]},{id:'BBB',positions:[{val:'CXO',label:'CXOXXX'},{val:'CEO',label:'CEOXXX'}]},{id:'CCC',positions:[{val:'CTO',label:'CTOXXX'}]}];


const occupied_positions = people

  .map( person => person.positions )

  .flat()

  .map( position => position.val );

  

const all_positions = positions

  .map( position => position.val );

  

const open_positions = all_positions

  .filter( position => !occupied_positions.includes( position ))

  .map( position => positions.find( source => source.val === position ));

  

console.log( open_positions );


查看完整回答
反对 回复 2021-04-29
?
缥缈止盈

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

您可以使用filter,find和some过滤掉那些不在people数组的positions数组中的对象。


var positions = [{val:'CEO',label:'CEOXXX'},{val:'CTO',label:'CTOXXX'},{val:'CBO',label:'CBOXXX'},{val:'CLO',label:'CLOXXX'}];

var people = [{id:'AAA',positions:[{val:'CEO',label:'CEOXXX'}]},{id:'BBB',positions:[{val:'CXO',label:'CXOXXX'},{val:'CEO',label:'CEOXXX'}]},{id:'CCC',positions:[{val:'CTO',label:'CTOXXX'}]}];


const out = positions.filter(position => {

  return !people.find(person => {

    return person.positions.some(({ val, label }) => {

      return val === position.val && label === position.label;

    });

  });

});


console.log(out);


查看完整回答
反对 回复 2021-04-29
  • 3 回答
  • 0 关注
  • 424 浏览
慕课专栏
更多

添加回答

举报

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