[英]How to filter array of objects with nested arrays of objects based on other arrays
[英]How to filter array of objects based on other array of objects?
我有 2 個對象數組:
Array1
Array2
我試圖通過僅獲取 productIds 與 array2 item productIds 相同的項目來過濾 Array1
const filteredArray = Array1.filter(item =>
Array2.includes(item.productId),
)
上述解決方案總是返回空數組。 出了什么問題,如何解決?
您需要尊重array2
的屬性,因為沒有對象等於另一個原始類型,如字符串或數字。
filteredArray = Array1.filter(one => Array2.some(two => one.productId === two.productId));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.