This is my answer to the problem. My underline version was 1.7, so I could not use .findIndex .
So I manually got the index of the item and replaced it. Here is the code for the same.
var students = [ {id:1,fName:"Ajay", lName:"Singh", age:20, sex:"M" }, {id:2,fName:"Raj", lName:"Sharma", age:21, sex:"M" }, {id:3,fName:"Amar", lName:"Verma", age:22, sex:"M" }, {id:4,fName:"Shiv", lName:"Singh", age:22, sex:"M" } ]
The method below will replace the student with id:4 with a lot of attributes in the object
function updateStudent(id) { var indexOfRequiredStudent = -1; _.each(students,function(student,index) { if(student.id === id) { indexOfRequiredStudent = index; return; }}); students[indexOfRequiredStudent] = _.extend(students[indexOfRequiredStudent],{class:"First Year",branch:"CSE"});
}
With underscore 1.8, this will be simplified since we have _.findIndexOf methods.
Sanjay Bharwani Mar 17 '17 at 14:18 2017-03-17 14:18
source share