猿问

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

我想过滤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数组中的任何对象表示。


倚天杖
浏览 425回答 3
3回答

慕容3067478

一种快速的方法是同时对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)

PIPIONE

执行我的评论。为了.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 );

缥缈止盈

您可以使用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);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答