我需要比較兩個陣列,這里是一個陣列 [...showid] 這里是另一個陣列 [...swipeeachotheruserId]
在此陣列中,某些 id 將包含此 id 相同但并非全部相同 [ 3, 5 , 7 , 10 ,14 ] [ 2, 5, 7, 1, 20, 14 ]
某些元素將相同但并非所有元素都相同
邏輯是
showid===swipeeachotheruserId ? 將排除不相同的 id 并在此處顯示:顯示 id
如何比較和排除此陣列值
uj5u.com熱心網友回復:
const showid = [ 3, 5, 7 , 10 ,14 ];
const swipeeachotheruserId = [ 2, 5, 7, 1, 20, 14 ];
const duplicates = showid.filter(value => swipeeachotheruserId.some(oneElement => oneElement === value));
const mergedArray = [
...showid,
...swipeeachotheruserId.filter(value => !duplicates.some(oneDuplicate => oneDuplicate === value))
];
const arrayWithoutDuplicate = mergedArray.filter(oneElem => !duplicates.some(value => oneElem === value));
console.log(duplicates);
console.log(mergedArray);
console.log(arrayWithoutDuplicate);
uj5u.com熱心網友回復:
如果要傳遞兩個陣列中的物件,請使用以下命令:
return showid.filter(e=>sipeeachotheruserid.includes(e))
如果要傳遞 swipeeachotheruserid 中沒有的物件,請使用以下命令:
return showid.filter(e=>!swipeeachotheruserid.includes(e))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381383.html
標籤:javascript 反应原生
