6

I have following array which consist of json objects:

items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}]

I have new json object. If the "Id" of this object(newItem) matches the "Id" in items, i would like to replace the "color" value.

newItem = {"Id":"car","color":"yellow"}

So, i would like output like this:

items = [{"Id":"car","color":"yellow"},{"Id":"truck","color":"red"}] 

Here's what i have so far:

for (var i=0; i<items.length; i++) {
    var x = items[i];

    if(x.Id == newItem.Id){    //first check if id matches  
        if (JSON.stringify(items[i]) == JSON.stringify(newItem))  { 
            alert('objects are same');                        
            return ;           
        } 
        else {
            var newColor = JSON.stringify(x).replace(x.color, newItem.color);
            alert(JSON.parse(newColor));

        }
    }            
}
1
  • $.each(items, function(i,item){ if(newItem.Id == item.Id) {item.color = newItem.color;}}); Commented Jul 30, 2015 at 18:13

6 Answers 6

9

Working JSFiddle. Here's one way to do it by using .forEach from Array.prototype.forEach:

var items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
var newItem = {"Id":"car","color":"yellow"}

items.forEach(function(item) {
  if (newItem.Id === item.Id) {
    item.color = newItem.color
  }
});
Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

items.forEach(function(item) {
    if(item.Id == newItem.Id && item.color != newItem.color){
        item.color = newItem.color;
    }            
});

Comments

2

Check working demo: JSFiddle.

Using Array.prototype.map is concise and clear:

items.map(function(obj) {
    (obj.Id === newItem.Id) && (obj.color = newItem.color);
});

Comments

2

The following code should work for you. You can exit the loop once the id is matched. A little better performance wise.

items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];

newItem = {"Id":"car","color":"yellow"};

for (var i = 0; i < items.length; i++){
    if (newItem.id == items[i].id ){
        items[i].color = newItem.color;
        break;
    }
}

Comments

2

Here's a way to do it, using jQuery:

var items = [
  {"Id": "car", "color": "blue"},
  {"Id": "truck", "color": "red"}
];

var newItem = {
  "Id": "car",
  "color": "yellow"
};


$(items).each(function(item) {
  if ($(this)[0].Id == newItem.Id) {
    $(this)[0].color = newItem.color
  }
}); 

Comments

2

The Underscore.js library isn't strictly necessary for this specific problem, but it's great for dealing with collections.

var items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
var newItem = {"Id":"car","color":"yellow"};

_.each(items, function(item) {
  if (item.Id === newItem.Id) item.color = newItem.color
});

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.