Get specific property values from an array of objects in JavaScript

← PrevNext →

Here’s another map() function example. In this post I’ll show you how to extract or get specific property values from an array of objects in JavaScript using the map function.

The JSON array

The array looks like this.

const oStock = [
    { 'company': 'Google', 'price': 1296.25, 'change': '+2.1' },
    { 'company': 'Cisco', 'price': 47.29, 'change': '-0.29' },
    { 'company': 'SBI', 'price': 399.00, 'change': '+5.85' }
  ];

I want to get only the company and price property values from the array, ignoring the change property value.

Here’s the script.

<script>
  const oStock = [
    { 'company': 'Google', 'price': 1296.25, 'change': '+2.1' },
    { 'company': 'Cisco', 'price': 47.29, 'change': '-0.29' },
    { 'company': 'SBI', 'price': 399.12, 'change': '+5.85' }
  ];

  const arr = oStock.map(stock => ' ' + stock.company + ' <b>' + stock.price + '</b>');
  document.getElementById('result').innerHTML = arr;
</script>
Try it

← PreviousNext →