【问题标题】:How to combine two array of objects into one array based on key match values如何根据键匹配值将两个对象数组组合成一个数组
【发布时间】:2019-02-06 07:49:37
【问题描述】:

我的第一个数组叫做 feeds:

[
   {
      _id: '5b8906e81248270685399a9a',
      name: 'fin24',
      state: 'OK',
      status: true,
    },
   { 
      _id: '5b8907031248270685399a9b',
      name: 'news24',
      state: 'OK',
      status: true,
  } 
]

我的第二个数组叫做 feedArticlesCount:

feedArticlesCount: [ 
  { _id: 5b8907031248270685399a9b,
    pending: 21,
    approved: 1,
    active: 21,
    inactive: 1 },
  { _id: 5b8906e81248270685399a9a,
    pending: 20,
    approved: 0,
    active: 20,
    inactive: 0 },
  { _id: 5b8664c26d90b107d0952cbe,
    pending: 62,
    approved: 8,
    active: 0,
    inactive: 70 },
  { _id: 5b865bf28152610775987d67,
    pending: 111,
    approved: 30,
    active: 0,
    inactive: 141 } 
]

我想根据匹配的 _id 值将这两个数组组合成一个数组,如果第一个数组中不存在 _id 我想跳过它。

我的预期输出:

[
   {
      _id: '5b8906e81248270685399a9a',
      name: 'fin24',
      state: 'OK',
      status: true,
      pending: 20,
      approved: 0,
      active: 20,
      inactive: 0 
    },
   { 
      _id: '5b8907031248270685399a9b',
      name: 'news24',
      state: 'OK',
      status: true,
      pending: 21,
      approved: 1,
      active: 21,
      inactive: 1 
  } 
]

我尝试了 forEach 和 reduce 方法,但它无法正常工作

我在这里尝试过:

let result = [];
feeds.forEach((itm, i) => {
  result.push(Object.assign({}, itm, feedArticlesCount[i]));
});

console.log(result);

这是可行的,但我的名字密钥存储在错误的 _id 位置。

【问题讨论】:

标签: javascript arrays json object


【解决方案1】:

因为 second 数组包含第一个数组中不存在的项目,所以您可以将第一个数组中的 .map 改为第二个数组中的匹配对象 .find

const arr=[{_id:'5b8906e81248270685399a9a',name:'fin24',state:'OK',status:!0,},{_id:'5b8907031248270685399a9b',name:'news24',state:'OK',status:!0,}];
const feedArticlesCount=[{_id:'5b8907031248270685399a9b',pending:21,approved:1,active:21,inactive:1},{_id:'5b8906e81248270685399a9a',pending:20,approved:0,active:20,inactive:0},{_id:'5b8664c26d90b107d0952cbe',pending:62,approved:8,active:0,inactive:70},{_id:'5b865bf28152610775987d67',pending:111,approved:30,active:0,inactive:141}]

console.log(arr.map(
  ({ _id, ...rest }) => ({ _id, ...rest, ...feedArticlesCount.find((item) => item._id === _id) })
));

为了降低复杂性,您可以先将feedArticlesCount 数组转换为以_id 为索引的对象(O(N) 而不是嵌套的.findO(N^2)):

const arr=[{_id:'5b8906e81248270685399a9a',name:'fin24',state:'OK',status:!0,},{_id:'5b8907031248270685399a9b',name:'news24',state:'OK',status:!0,}];
const feedArticlesCount=[{_id:'5b8907031248270685399a9b',pending:21,approved:1,active:21,inactive:1},{_id:'5b8906e81248270685399a9a',pending:20,approved:0,active:20,inactive:0},{_id:'5b8664c26d90b107d0952cbe',pending:62,approved:8,active:0,inactive:70},{_id:'5b865bf28152610775987d67',pending:111,approved:30,active:0,inactive:141}]

const articlesById = feedArticlesCount.reduce((a, { _id, ...rest }) => {
  a[_id] = rest;
  return a;
}, {});

console.log(arr.map(
  ({ _id, ...rest }) => ({ _id, ...rest, ...articlesById[_id] })
));

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2017-02-11
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多