2

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?

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}
]
3
  • 1
    What have you tried so far? Commented Oct 16, 2018 at 10:18
  • I have tried to use nested loops but it is returning the elements only same ids. Commented Oct 16, 2018 at 10:21
  • @hackrack, pls mark the answer as an accepted solution if it helped you achieve what you were looking for. Thanks. Commented Oct 16, 2018 at 10:52

5 Answers 5

2

You can do that with forEach and filter as below:

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

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

Comments

2

This can be done as follows. Also this is generic and will concat all properties for same news_id, not just count and title.

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) ,{})))

2 Comments

Yes, @NickParsons I intentionally did generic approach so that other properties can also be taken care of. Though I did add "count" as well there to be more specific.
Ah yes, sorry didn't see your comment.
1

In your case to get the desired result.I recommend you should try old school JS forEach() Method.

And here is the example done check it on 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;
  }
});

Comments

0

Here is a simpler solution. I simply iterate arr1 and add the count from the matching arr2 item, if any.

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.

Comments

0

You could take a Map for collecting all given data and the counts. 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; }

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.