Alistair
Alistair

Reputation: 641

Replace element that has changed in array

I have an array of attendees, 2 of them are also instructors. I want to update one of the instructors by replacing him/her and leave the remaining attendees intact in the array.

Here's an example:

    {   
      attendees: [ 
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' } 
      ]
    }

Now I submit a new array of instructors with one of them changed:

    {
      instructors: [
        { email : '[email protected]' },
        { email : '[email protected]' }
      ]
    }

And my final result should be:

    {   
      attendees: [ 
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' } 
      ]
    }

Where [email protected] has replaced [email protected] as the new instructor. I think I can use _.differenceBy with lodash but can't figure out how to replace the changed element in the array. Is there an elegant way to do this?

Upvotes: 0

Views: 58

Answers (2)

Oscar Gonzalez
Oscar Gonzalez

Reputation: 142

Here is a few solutions that either 1) puts the updates in a new variable or 2) updates the attendees variable. Of course, this is pretty limited because your data doesn't have something akin to a primary key (ie: an ID field). If you do have a primary key, then you can modify these examples to check the id.

var attendees = [ 
    { email: '[email protected]' },
    { email: '[email protected]' },
    { email: '[email protected]' },
    { email: '[email protected]' },
    { email: '[email protected]' } 
]

var instructors = [
    { email : '[email protected]' },
    { email : '[email protected]' }
]

// 1) in a new variable
var updatedAttendees = attendees.map(function(item, index) {
    return instructors[index] || item;
})

// 2) In the same variable
for (var i = 0; i < attendees.length; i++) {
    if (instructors[i]) {
        attendees[i] = instructors[i];
    }
}

If you did have a primary key, it might look like this. Note that we now have two nested loops. This example is not optimized at all, but just to give you the general idea:

var attendeesWithId = [
    { id: 1, email: '[email protected]' },
    { id: 2, email: '[email protected]' },
    { id: 3, email: '[email protected]' },
    { id: 4, email: '[email protected]' },
    { id: 5, email: '[email protected]' } 
]

var updates = [
    { id: 4, email: '[email protected]' },
]

for (var j = 0; j < updates.length; j++) {
    var update = updates[j];

    for (var i = 0; i < attendeesWithId.length; i++) {
        if (update.id === attendeesWithId[i].id) {
            attendeesWithId[i] = update;
        }
    }
}

Upvotes: 1

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18997

Does this help

var initialData =  {   
      attendees: [ 
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' },
        { email: '[email protected]' } 
      ]
    }

var updateWithThis = {
      instructors: [
        { email : '[email protected]' },
        { email : '[email protected]' }
      ]
    }

for(var i=0; i< updateWithThis.instructors.length;i++){
    initialData.attendees[i] = updateWithThis.instructors[i];
}

document.write(JSON.stringify(initialData));

Upvotes: 0

Related Questions