简体   繁体   English

如何在JavaScript中将两个不同的对象数组合并为一个新的对象数组?

[英]how to combine two different array of objects into one new array of object in javascript?

I have two arrays of objects with same news_ids but different properties, I am just wondering how to combine them and get a new array of objects? 我有两个对象数组,它们具有相同的news_id,但属性不同,我只是想知道如何组合它们并获得新的对象数组?

For example: 例如:

let arr1 = [{
    news_id: 1,
    title: "title1"
  },
  {
    news_id: 2,
    title: "title2"
  },
  {
    news_id: 3,
    title: "title3"
  },
  {
    news_id: 4,
    title: "title4"
  },
]

let arr2 = [{
    news_id: 3,
    count: 3
  },
  {
    news_id: 4,
    count: 4
  }
]

And I would like to get: 我想得到:

[
  {news_id: 1, title: "title1", count: 0},
  {news_id: 2, title: "title2", count: 0},
  {news_id: 3, title: "title3", count: 3},
  {news_id: 4, title: "title4", count: 4}
]

This can be done as follows. 这可以如下进行。 Also this is generic and will concat all properties for same news_id, not just count and title. 同样,这是通用的,它将为相同的news_id连接所有属性,而不仅仅是计数和标题。

let arr1 = [
              {news_id: 1, title: "title1"},
              {news_id: 2, title: "title2"},
              {news_id: 3, title: "title3"},
              {news_id: 4, title: "title4"},
           ]


let arr2 = [
             {news_id: 3, count: 3},
             {news_id: 4, count: 4}
           ]

let result = Object.values(([...arr1, ...arr2].reduce((acc, d) => (acc[d.news_id] = { count:0, ...acc[d.news_id], ...d }, acc) ,{})))

You can do that with forEach and filter as below: 您可以使用forEach并按如下所示进行filter

arr1.forEach(i => i.count = (arr2.find(j => j.news_id == i.news_id) || { count: 0 }).count)

Try it below. 请在下面尝试。

 let arr1 = [{ news_id: 1, title: "title1" }, { news_id: 2, title: "title2" }, { news_id: 3, title: "title3" }, { news_id: 4, title: "title4" }, ] let arr2 = [{ news_id: 3, count: 3 }, { news_id: 4, count: 4 } ] arr1.forEach(i => i.count = (arr2.find(j => j.news_id == i.news_id) || { count: 0 }).count); console.log(arr1); 

In your case to get the desired result.I recommend you should try old school JS forEach() Method . 在您的情况下,以获得理想的结果。我建议您尝试使用老式的JS forEach()方法

And here is the example done check it on JS Fiddle Example 这是完成的示例,请在JS Fiddle示例中进行检查

let arr1 = [{
    news_id: 1,
    title: "title1"
  },
  {
    news_id: 2,
    title: "title2"
  },
  {
    news_id: 3,
    title: "title3"
  },
  {
    news_id: 4,
    title: "title4"
  }
]

let arr2 = [
    {
    news_id: 3,
    count: 3
  },
  {
    news_id: 4,
    count: 4
  }
]


arr1.forEach(function (e,i) {
    var flag = false;
  arr2.forEach(function (obj, j) {
        if (e.news_id === obj.news_id) {
            e.count = obj.count;
      flag = true;
        }
    });
  if(!flag){
    e.count = 0;
  }
});

Here is a simpler solution. 这是一个更简单的解决方案。 I simply iterate arr1 and add the count from the matching arr2 item, if any. 我只是简单地迭代arr1并添加匹配的arr2项(如果有)中的count

for (item of arr1) {
    let arr2item = arr2.find(item2 => item2.news_id === item.news_id);
  item['count'] = arr2item ? arr2item.count : 0;
};

@Nitish's answer looks good, but the intention is not very clear. @Nitish的回答看起来不错,但是意图不是很清楚。

You could take a Map for collecting all given data and the counts. 您可以使用Map来收集所有给定的数据和计数。 Then render the final result. 然后渲染最终结果。

 var array1 = [{ news_id: 1, title: "title1" }, { news_id: 2, title: "title2" }, { news_id: 3, title: "title3" }, { news_id: 4, title: "title4" }], array2 = [{ news_id: 3, count: 3 }, { news_id: 4, count: 4 }], result = Array.from( array2 .reduce( (m, { news_id, count }) => (m.get(news_id).count += count, m), array1.reduce((m, o) => m.set(o.news_id, Object.assign({}, o, { count: 0 })), new Map) ) .values() ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

相关问题 如何根据Javascript中的属性组合两个不同大小的对象数组? - How to combine two array of objects of different sizes, based on a property in Javascript? 在 JavaScript 中使用两个不同的 object 创建一个新的数组对象 - Make a new array objects using two different object in JavaScript 如何在javascript中将两个数组组合成一个对象数组? - How to combine two arrays into an array of objects in javascript? Javascript组合两个对象数组 - Javascript combine two array of objects 将一组对象组合成一个 OBJECT - Combine an array of objects into one OBJECT 如何在对象数组中组合类似的对象并插入新的 object? - How to combine like objects within array of objects and insert new object? JavaScript 中的两个不同阵列并组合单个阵列 - Two different array in JavaScript and combine Single array JS:如何将来自两个不同数组的对象合并为一个数组/对象 - JS: How to combine Object from two different arrays into one Array/Object 我可以使用React和Javascript将这两个数组组合成一个对象吗? - Can I combine these two array into one object using React and Javascript? 比较两个数组对象数据并在JavaScript中将一个对象合并为一个数组? - Comparing two array objects data and merge as one object into array in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM