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

时间:2018-10-16 10:17:19

标签: javascript

我有两个对象数组,它们具有相同的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
  }
]

我想得到:

[
  {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}
]

5 个答案:

答案 0 :(得分:2)

这可以如下进行。同样,这是通用的,它将为相同的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) ,{})))

答案 1 :(得分:2)

您可以使用forEachfilter进行以下操作:

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

在下面尝试。

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);

答案 2 :(得分:1)

要取得理想的效果,我建议您尝试旧式的JS forEach() Method

这是在JS Fiddle 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
  }
]


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;
  }
});

答案 3 :(得分:0)

这是一个更简单的解决方案。我只是简单地迭代Summary of all failing tests FAIL src/containers/initiatives/InitiativeCampaignContainer.test.jsx ● <InitiativeCampaignContainer/> › renders one add campaign component TypeError: validate is not a function 140 | 141 | /* eslint-disable function-paren-newline */ > 142 | const wrapper = mount( 143 | <Provider store={store}> 144 | <AddCampaignForm validate={[validationRules]} /> 145 | </Provider>并从匹配的arr1项目(如果有)中添加count

arr2

@Nitish's answer看起来不错,但意图并不十分明确。

答案 4 :(得分:0)

您可以使用Map来收集所有给定的数据和计数。然后呈现最终结果。

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; }

相关问题