我有一个数组尝试使用lodash过滤。最终目标是从数组中返回属性值不在另一个数组中的所有对象。

let inUse = ['1','2'];
let positionData = [{
    fieldID: '1',
    fieldName: 'Test1'
},
{
    fieldID: '2',
    fieldName: 'Test2'
},
{
    fieldID: '3',
    fieldName: 'Test3'
}]

// Only show me position data where the fieldID is not in our inUse array
const original = _.filter(positionData, item => item.fieldID.indexOf(inUse) === -1);


我尝试使用indexOf,但在这种情况下我认为使用的不正确。

预期结果:

original = {
 fieldID: '3',
 fieldName: 'Test3'
}

最佳答案

看来您的indexOf倒退了。目前,它正在inUse中寻找item.fieldID

尝试这个:

const original = _.filter(positionData, item => inUse.indexOf(item.fieldID) === -1);

10-05 18:40