简体   繁体   English

JavaScript 如何通过两个不同的 arrays 对象并有条件地返回一个数组

[英]JavaScript how to map through two different arrays of objects and return one array conditionally

hello I need help creating the newProducts in the following example array dynamically without knowing what's in the other two arrays https://jsfiddle.net/od4267fv/ more explanation:您好,我需要帮助在以下示例数组中动态创建 newProducts,而不知道其他两个 arrays https://jsfiddle.net/od4267fv/中的内容:

I would like to check我想检查

products = [
  {
    id: 1,
    name: 'product 1' ,
    liked:  false
  },
{
    id: 2,
    name: 'product 2' ,
    liked:  false
  },
  {
    id: 3,
    name: 'product 3' ,
    liked:  false
  },
  {
    id: 4,
    name: 'product 4' ,
    liked:  false
  },
  {
    id: 5,
    name: 'product 5' ,
    liked:  false
  },

]
likedProducts = [
   {
    id: 1,
    name: 'product 1' ,
    liked:  true
  },
{
    id: 2,
    name: 'product 2' ,
    liked:  true
  },
  {
    id: 3,
    name: 'product 3' ,
    liked:  true
  },

]
if proucts[product].id === likedProducts[product].id
newProducts.push(likedProducts[product])
else
newProducts.push(proucts[product])

please recommend suitable ways to do it请推荐合适的方法

Try this尝试这个

const allProducts = products.filter(n => likedProducts.find(n2 => n.id == n2.id));

You can do the following using map() and findIndex() ,您可以使用map()findIndex()执行以下操作,

 products = [ { id: 1, name: 'product 1', liked: false }, { id: 2, name: 'product 2', liked: false }, { id: 3, name: 'product 3', liked: false }, { id: 4, name: 'product 4', liked: false }, { id: 5, name: 'product 5', liked: false }, ] likedProducts = [ { id: 1, name: 'product 1', liked: true }, { id: 2, name: 'product 2', liked: true }, { id: 3, name: 'product 3', liked: true }, ] newProducts = products.map(item => { let index = likedProducts.findIndex(likedProduct => likedProduct.id === item.id); if(index > -1) { return likedProducts[index]; } return item; }) console.log(newProducts);

This is the way to iterate over 2 array and update value upon some certain condition.这是在某些特定条件下迭代 2 个数组并更新值的方法。

 const updatedProduct = proucts.map((row, indexOfRows) => { for (let index = 0; index < likedProducts.length; index++) { if (likedProducts[index].id === row.id) { return likedProducts[index]; } } return row; });

You can do a single iteration over each of the arrays.您可以对每个 arrays 进行一次迭代。 First index all products by their id, then iterate over the liked products and overwrite the indexed items.首先按 id 索引所有产品,然后遍历喜欢的产品并覆盖索引的项目。 This way, it doesn't matter how many items each array has, nor do you need to do constant lookups into at least one of the arrays.这样,每个数组有多少项都没有关系,也不需要对至少一个 arrays 进行持续查找。

 const products = [{id: 1,name: 'product 1',liked: false},{id: 2,name: 'product 2',liked: false},{id: 3,name: 'product 3',liked: false},{id: 4,name: 'product 4',liked: false},{id: 5,name: 'product 5',liked: false},]; const likedProducts = [{id: 1,name: 'product 1',liked: true},{id: 2,name: 'product 2',liked: true},{id: 3,name: 'product 3',liked: true},]; const index = new Map(); //index all products for (let item of products) { index.set(item.id, item); } //overwrite anything that is liked for (let likedItem of likedProducts) { if (index.has(likedItem.id)) { index.set(likedItem.id, likedItem); } } //construct a new array from the values const result = Array.from(index.values()); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Map 通过 Javascript 对象数组并返回一个满足条件的新对象 - Map through a Javascript array of objects and return a new one satisfying a condition 如何在javascript中将两个相同长度的数组转换为一个对象数组? - How to convert two arrays of same length into one array of objects in javascript? 如何将两个数组中的对象映射为一个? - How to map objects in two arrays into one? 在一个对象数组中结合两个不同对象数组的不同属性 - Combining different properties of two different arrays of objects in one array of objects 如何从两个 arrays 返回对象数组 - How to return array of objects from two arrays 如何映射两个不同长度的数组? (JavaScript)的 - How to map two arrays with different lengths? (Javascript) 如何比较javascript中两个不同arrays的对象 - How to compare objects of two different arrays in javascript 如何在javascript中将两个数组组合成一个对象数组? - How to combine two arrays into an array of objects in javascript? 如何在对象数组上使用jQuery.map()来返回数组数组 - How to use jQuery.map() on array of objects to return array of arrays 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM