簡體   English   中英

如何根據其他對象數組過濾對象數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM