get data from objects of an array - Javascript Array Operation

Javascript examples for Array Operation:Array of Object

Description

get data from objects of an array

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  .j  a v  a2  s.co m
var data = [{
  name: "coke",
  price: "10"
}, {
  name: "sprit",
  price: "20"
}];
function getValue(data, name) {
  for (let i = 0; i < data.length; i++) {
    if (data[i].name === name) {
      return data[i].price;
    }
  }
}
var value = getValue(data, 'coke');
console.log(value);
    }

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

Related Tutorials