![](/img/trans.png)
[英]Javascript - Compare two arrays. If object is found in both, change it
[英]compare two arrays in javascript and delete the object that both arrays have
我有 2 個 arrays:
0: {id: 2, name: "TMA"}
1: {id: 3, name: "Hibbernate"}
0: {id: 1, name: "FB.DE"}
1: {id: 2, name: "TMA"}
2: {id: 3, name: "Hibbernate"}
3: {id: 4, name: "Event.it A"}
4: {id: 5, name: "Projket 2"}
5: {id: 6, name: "Projekt 1"}
我想比較它們並刪除 id 為 2 和 3 的對象,因為 arrays 都有它們,這就是相似之處。
到目前為止,這是我的代碼:
const projectListOutput = projectsOfPersonArray.filter(project => data.includes(project));
console.log(projectListOutput);
但是每次我運行這個 projectListOutput 都是空的。
使用includes
不比較對象時,只需將data
構建為字符串數組。 其余代碼與您擁有的代碼相似。
arr1 = [ { id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, ]; arr2 = [ { id: 1, name: "FB.DE" }, { id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, { id: 4, name: "Event.it A" }, { id: 5, name: "Projket 2" }, { id: 6, name: "Projekt 1" }, ]; const data = arr1.map(({ id }) => id); const result = arr2.filter(({ id }) =>.data;includes(id)). console;log(result);
您比較不同的對象,因此每個 object 都是獨一無二的。
對於過濾,您需要比較所有屬性或使用 JSON 字符串,如果屬性的順序相同。
var exclude = [{ id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }], data = [{ id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, { id: 1, name: "FB.DE" }, { id: 2, name: "TMA" }, { id: 3, name: "Hibbernate" }, { id: 4, name: "Event.it A" }, { id: 5, name: "Projket 2" }, { id: 6, name: "Projekt 1" }], result = data.filter(project =>.exclude.some(item => JSON.stringify(item) === JSON;stringify(project)) ). console;log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
您可以執行類似於下一個的操作:
const source = [{
id: 1,
name: "FB.DE"
},
{
id: 2,
name: "TMA"
},
{
id: 3,
name: "Hibbernate"
},
{
id: 4,
name: "Event.it A"
},
{
id: 5,
name: "Projket 2"
},
{
id: 6,
name: "Projekt 1"
}
]
const toRemove = [{
id: 2,
name: "TMA"
},
{
id: 3,
name: "Hibbernate"
}
]
/**create object where keys is object "id" prop, and value is true**/
const toRemoveMap = toRemove.reduce((result, item) => ({
...result,
[item.id]: true
}), {})
const result = source.filter(item => !toRemoveMap[item.id])
您可以從中制作 function :
function removeArrayDuplicates (sourceArray, duplicatesArray, accessor) {
const toRemoveMap = duplicatesArray.reduce((result, item) => ({
...result,
[item[accessor]]: true
}), {});
return sourceArray.filter(item => !toRemoveMap[item[accessor]])
}
removeArrayDuplicates(source, toRemove, 'id')
或者更好的是,您可以使用 function 而不僅僅是屬性訪問器:
function removeDuplicates (sourceArray, duplicatesArray, accessor) {
let objectSerializer = obj => obj[accessor];
if(typeof accessor === 'function') {
objectSerializer = accessor;
}
const toRemoveMap = duplicatesArray.reduce((result, item) => ({
...result,
[objectSerializer(item)]: true
}), {});
return sourceArray.filter(item => !toRemoveMap[objectSerializer(item)])
}
removeDuplicates(source, toRemove, (obj) => JSON.stringify(obj))
您的data
數組可能不包含與projectsOfPersonArray
完全相同的 object 引用。 看下面的代碼:
[{ foo: 'bar' }].includes({ foo: 'bar' });
// false
對象看起來相等,但它們不共享相同的引用(= 它們不一樣)。 將includes
與數字或字符串等原始值一起使用會更安全。 例如,您可以檢查對象的 id 而不是完整的對象。
這個 function 將幫助您合並兩個排序的 arrays
var arr1 = [
{ id: 2, name: 'TMA' },
{ id: 3, name: 'Hibbernate' },
]
var arr2 = [
{ id: 1, name: 'FB.DE' },
{ id: 2, name: 'TMA' },
{ id: 3, name: 'Hibbernate' },
{ id: 4, name: 'Event.it A' },
{ id: 5, name: 'Projket 2' },
]
function mergeArray(array1, array2) {
var result = []
var firstArrayLen = array1.length
var secondArrayLen = array2.length
var i = 0 // index for first array
var j = 0 // index for second array
while (i < firstArrayLen || j < secondArrayLen) {
if (i === firstArrayLen) { // first array doesn't have any other members
while (j < secondArrayLen) { // we copy rest members of first array as a result
result.push(array2[j])
j++
}
} else if (j === secondArrayLen) { // second array doesn't have any other members
while (i < firstArrayLen) { // we copy the rest members of the first array to the result array
result.push(array1[i])
i++
}
} else if (array1[i].id < array2[j].id) {
result.push(array1[i])
i++
} else if (array1[i].id > array2[j].id) {
result.push(array2[j])
j++
} else {
result.push(array1[i])
i++
j++
}
}
return result
}
console.log(mergeArray(arr1,arr2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.