0

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.

3
  • by id in wrong place do you mean not first ? I can't see the problem with that Commented Aug 31, 2018 at 9:49
  • @jonatjano jsbin.com/wufiravefu/edit?js,output check this Commented Aug 31, 2018 at 9:50
  • name is getting in wrong place Commented Aug 31, 2018 at 9:50

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.