2017-08-09 74 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);