Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}]
  2.  
  3. newItem = {"Id":"car","color":"yellow"}
  4.  
  5. items = [{"Id":"car","color":"yellow"},{"Id":"truck","color":"red"}]
  6.  
  7. for (var i=0; i<items.length; i++)
  8. {
  9. var x = items[i];
  10.  
  11. if(x.Id == newItem.Id){ //first check if id matches
  12. if (JSON.stringify(items[i]) == JSON.stringify(newItem)) {
  13. alert('objects are same');
  14. return ;
  15. }
  16. else {
  17. var newColor = JSON.stringify(x).replace(x.color, newItem.color);
  18. alert(JSON.parse(newColor));
  19.  
  20. }
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement