Sameer
Sameer

Reputation: 3496

How to combine two array of objects into one array based on key match values

My first array called feeds:

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

My second Array called 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 } 
]

I want to combine these both two arrays into one based on matching _id value, if _id not exist in first array i want to skip that.

My Expected output:

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

I tried with forEach and reduce method, but its not working correctly

Here i tried:

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

console.log(result);

This is working but my name key is storing in wrong _id place.

Upvotes: 0

Views: 38

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370619

Because it's the second array that contains items that don't exist in the first array, you can .map by the first array instead, and .find the matching object in the second array:

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

For less complexity, you can transform the feedArticlesCount array into an object indexed by _id first (O(N) rather than a nested .find's O(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] })
));

Upvotes: 2

Related Questions