Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested forEach loops to add object to existing object javascript

Is there a better way to iterate over two array of objects than what I have done below? It seems messy to do it this way. I'm using lodash.

var array1 = [
   {id:4356, name: 'James', sex: 'male'}, 
   {id:7899, name: 'Jimmy', sex: 'male'}, 
   {id:2389, name: 'Dawn', sex: 'female'}
];

var array2 = [
    {id:4356, salary: 1000, job: 'programmer'}, 
    {id:7899, salary: 2000, job: 'tester'}, 
    {id:2389, salary: 3000, job: 'manager'}
];

Example output:

console.log(array1[0])
{
    id:4356, 
    name: James, 
    sex: male, 
    person: {
        id:4356, 
        salary: 1000, 
        job: programmer
    }
}

Function:

_.forEach(array1, function(item1) {
    _.forEach(array2, function(item2) {
       if(item1.id === item2.id){
          item1.person = item2;
        }
     });
});
like image 544
tester123 Avatar asked Jan 14 '16 19:01

tester123


People also ask

Can forEach loops be nested?

An important feature of foreach is the %:% operator. I call this the nesting operator because it is used to create nested foreach loops. Like the %do% and %dopar% operators, it is a binary operator, but it operates on two foreach objects.

Can you use a forEach loop on an object?

JavaScript's Array#forEach() function lets you iterate over an array, but not over an object. But you can iterate over a JavaScript object using forEach() if you transform the object into an array first, using Object. keys() , Object. values() , or Object.

Can you use forEach on an array of objects?

The forEach method is also used to loop through arrays, but it uses a function differently than the classic "for loop". The forEach method passes a callback function for each element of an array together with the following parameters: Current Value (required) - The value of the current array element.

Can we use for loop inside forEach loop in JavaScript?

forEach loops accept a callback function whereas for loops do not. In a for loop, all of your code is enclosed in the main body of the loop. In a forEach loop, you must write a function which will be executed for each item in the list over which you are iterating.


1 Answers

Since you're using lodash, you could use the _.find() method to find the corresponding object in array2 based on the id properties.

_.forEach(array1, function(item1) {
    item1.person = _.find(array2, {id: item1.id});
});

It's worth pointing out that this will result in an undefined person property if an object isn't found. If that's a problem, simply check to see if an object is returned:

_.forEach(array1, function(item1) {
    var obj = _.find(array2, {id: item1.id});
    if (obj) {
        item1.person = obj;
    }
});

Without lodash, it would be pretty similar:

array1.forEach(function(item1) {
    item1.person = array2.find(function (item2) {
      return item2.id === item1.id;
    });
});
like image 184
Josh Crozier Avatar answered Oct 23 '22 04:10

Josh Crozier