2017-08-09 71 views
2

给定两个阵列:(onetwo)。获得价值不在阵列

one:包含的值

two:包含值

我需要从one获取值其不在two对象。我试过.filter().indexOf(),但没有得到理想的结果。

在以下情况下,我希望result的值为333。如何实现这一目标?

var one = [111, 222, 333, 444]; 
 
var two = [ 
 
    { identifier: 111 }, 
 
    { identifier: 222 }, 
 
    { identifier: 444 } 
 
]; 
 

 
var result = two.filter(function (item) { 
 
    console.log(item.identifier, one.indexOf(item.identifier)); 
 
}); 
 

 
console.log('result: ', result);

+1

[滤波器阵列不在另一个阵列]的可能的复制(https://stackoverflow.com/questions/33577868/filter-array-not-in-another-array) –

+1

或者https://stackoverflow.com/questions/2963281/javascript-algorithm-to-find-elements-in-array-that-are-not-in-another-array可能更好 –

回答

1

只要做你想做的,过滤one并采取只有那些不在two(发现它在two,如果发现它会返回对象即true等价的,如果找不到,则它将返回undefinedfalse当量和否定!它)

var one = [111, 222, 333, 444]; 
 
var two = [ 
 
    { identifier: 111 }, 
 
    { identifier: 222 }, 
 
    { identifier: 444 } 
 
]; 
 
var resultArr = one.filter(function(val){ 
 
    return !two.find(function(obj){ 
 
     return val===obj.identifier; 
 
    }); 
 
}); 
 

 
console.log(resultArr)

2

我会从two提取标识符值,然后经one运行过滤器:

var one = [111, 222, 333, 444]; 
 
var two = [ 
 
    { identifier: 111 }, 
 
    { identifier: 222 }, 
 
    { identifier: 444 } 
 
]; 
 

 
// get a flattened array of identifier values [111, 222, 444] 
 
const identifiers = two.map((item) => item.identifier); 
 

 
var result = one.filter(function (item) { 
 
    console.log(item, identifiers.indexOf(item) === -1); 
 
    return identifiers.indexOf(item) === -1; 
 
}); 
 

 
console.log('result: ', result);

+0

看起来很复杂,但给出了正确的结果!谢谢! +1 – caramba

+0

如果'two'包含一个不在'one'中的项目,这仍然有效吗?,对不起,没关系重读这个问题,并且看到他想要'one'中的那个。 – Quince

+0

@Quince yes它会产生相同的输出。 – Dario

0

可以映射可变两个第一

var one = [111, 222, 333, 444]; 
 
var two = [ 
 
    { identifier: 111 }, 
 
    { identifier: 222 }, 
 
    { identifier: 444 } 
 
]; 
 
two = two.map(function(item) { 
 
    return item.identifier; 
 
}); 
 

 
var result = one.filter(function (item) { 
 
    return two.indexOf(item) == -1; 
 
}); 
 

 
console.log('result: ', result);

0

您可以使用数组2中的所有值创建一个临时数组。然后使用的indexOf检查是否从一个数组的任意值在数组想着2

var one = [111, 222, 333, 444]; 
 
var two = [{ 
 
    identifier: 111 
 
    }, 
 
    { 
 
    identifier: 222 
 
    }, 
 
    { 
 
    identifier: 444 
 
    } 
 
]; 
 
var tempArray = [] 
 
two.forEach(function(item) { 
 
    tempArray.push(item.identifier) 
 

 
}) 
 
one.forEach(function(item) { 
 
    if (tempArray.indexOf(item) === -1) { 
 
    console.log(item) 
 
    } 
 
})

1

可以过滤返回未在阵列two发现元素的数组one

代码:

const one = [111, 222, 333, 444]; 
 
const two = [{identifier: 111},{identifier: 222},{identifier: 444}]; 
 
const result = one.filter(oneElem => !two.find(twoElem => oneElem === twoElem.identifier)); 
 

 
console.log('result: ', result);

2

你不return.filter()一个Boolean值。

可以遍历one阵列,并且可以使用.some()!运营商来检查当前值存在于two阵列"identifier"财产

var one = [111, 222, 333, 444]; 
 
var two = [ 
 
    { identifier: 111 }, 
 
    { identifier: 222 }, 
 
    { identifier: 444 } 
 
]; 
 

 
var result = one.filter(function (item) { 
 
    return !two.some(function(el) { 
 
     return el.identifier === item 
 
    }) 
 
}); 
 

 
console.log('result: ', result);

0
var one = [111, 222, 333, 444]; 
var two = [ 
    { identifier: 111 }, 
    { identifier: 222 }, 
    { identifier: 444 } 
]; 

vals = two.map(e=>{ return e['identifier']}) 

val = one.filter(e => { return vals.indexOf(e) == -1}) 
console.log(val) 

映射值变为数组,然后过滤第一个数组。

1

您可以使用Set并过滤one的值。

var one = [111, 222, 333, 444], 
 
    two = [{ identifier: 111 }, { identifier: 222 }, { identifier: 444 } ], 
 
    result = one.filter((s => a => !s.has(a))(new Set(two.map(o => o.identifier)))); 
 

 
console.log(result);