所以,我得到了:
array1 =
[
["Fisrt Name1", "Last Name1", "[email protected]"],
["Fisrt Name2", "Last Name2", "[email protected]"],
["Fisrt Name3", "Last Name2", "[email protected]"]
]
array2 =
[
["[email protected]"],
["[email protected]"],
]
我正在嘗試比較它們并array1通過以下方式保持唯一性,它應該回傳:
resultingArray =
[
["Fisrt Name1", "Last Name1", "[email protected]"],
["Fisrt Name3", "Last Name2", "[email protected]"]
]
...但它正在回傳一切。
嘗試1:
resultingArray = array1.filter(e => !array2.includes(e));
嘗試2:
let resultingArray = [];
for (let a = 0; a < array1.length; a ){
for (let n= 0; n < array2.length; n ){
if(array1[a][2].indexOf(array2[n][0]) === -1){
resultingArray.push(array1[a])
}
}
}
感謝您的幫助!
uj5u.com熱心網友回復:
這是重構代碼后我最終編輯的答案。
array1 = [
['Fisrt Name1', 'Last Name1', '[email protected]'],
['Fisrt Name2', 'Last Name2', '[email protected]'],
['Fisrt Name3', 'Last Name3', '[email protected]'],
['Fisrt Name4', 'Last Name4', '[email protected]'],
];
array2 = [['[email protected]'], ['[email protected]']];
let resultingArray = [];
first: for (let a = 0; a < array1.length; a ) {
second: for (let n = 0; n < array2.length; n ) {
if (array1[a][2] === array2[n][0]) {
continue first;
}
}
resultingArray.push(array1[a]);
}
console.log(resultingArray);
在這里,我使用了標簽。更多關于標簽。
uj5u.com熱心網友回復:
您不能比較 2 個參考值(陣列或物件)可能類似于
array1 = [
['Fisrt Name1', 'Last Name1', '[email protected]'],
['Fisrt Name2', 'Last Name2', '[email protected]'],
['Fisrt Name3', 'Last Name3', '[email protected]'],
['Fisrt Name4', 'Last Name4', '[email protected]'],
];
array2 = [['[email protected]'], ['[email protected]']];
const result = array2.map(item => array1.filter(e => !e.includes(item[0])))
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/445625.html
