Vishal Seth Vishal Seth - 1 year ago 85
Javascript Question

is there a function in lodash to replace matched item

I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible duplicate but I did not understand the answer there:)

I looked at their documentation but could not find anything

My code is:

var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];
// Can following code be reduced to something like _.XX(arr, {id:1}, {id:1, name: "New Name"});
_.each(arr, function(a, idx){
if( === 1){
arr[idx] = {id:1, name: "Person New Name"};
return false;

_.each(arr, function(a){

The object I'm trying to replace with has many properties like

{id: 1, Prop1: ..., Prop2:..., and so on}


Thanks to dfsq but I found a proper solution within lodash that seems to work fine and is pretty neat and I put it in a mixin as well since I've this requirement at many places. JSBin

var update = function(arr, key, newval) {
var match = _.find(arr, key);
_.merge(match, newval);

_.mixin({ '$update': update });

var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];

_.$update(arr, {id:1}, {id:1, name: "New Val"});


Faster Solution
As pointed out by @dfsq, following is way faster

var upsert = function (arr, key, newval) {
var match = _.find(arr, key);
var index = _.indexOf(arr, _.find(arr, key));
arr.splice(index, 1, newval);
} else {

Answer Source

In your case all you need to do is to find object in array and use Array.prototype.splice method:

var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];

// Find item index using indexOf+find
var index = _.indexOf(arr, _.find(arr, {id: 1}));

// Replace item at index using native splice
arr.splice(index, 1, {id: 100, name: 'New object.'});

// "console.log" result
document.write(JSON.stringify( arr ));
<script src="//"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download