Use array map function to replace value if key is same in json object - Javascript JSon

Javascript examples for JSon:JSon String

Description

Use array map function to replace value if key is same in json object

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//www  . ja va2s  .c  o m
var items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
var newItem = {"Id":"car","color":"yellow"};
items.map(function(obj) {
    (obj.Id === newItem.Id) && (obj.color = newItem.color);
});
console.log(items);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials