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

时间:2019-04-12 13:24:17

标签: javascript lodash

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您可以使用filterfindsome来过滤掉不在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);

答案 2 :(得分:1)

我的评论的实现。

整个事情可以写成一个大的.reduce()到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 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 );