0

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?

4
  • Array#concat(Array) Commented Apr 7, 2016 at 12:05
  • 2
    This doesn't seem to make sense. In the original set the only way to know something is an instructor is by inspecting the email. After the replacement, there's no way to know that 'test' is an instructor. Is that what you want? Commented Apr 7, 2016 at 12:11
  • 1
    Your attendee and instructor arrays would need an extra attribute for each person, like id, where they can be identified with and matched across the 2 arrays. Commented Apr 7, 2016 at 12:14
  • Do the atteendees and instructors share the same index? Eg. attendees[0] === instructors[0]? In that case something like this jsfiddle.net/wyL19ojr ? Commented Apr 7, 2016 at 12:15

2 Answers 2

1

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.