Skip to content

Extract array from object JavaScript | Exmaple code

  • by

The simplest thing to do would be to use map() to Extract an array from an object in JavaScript. Then you can loop through it or build a string from it as required.

Here is a shorter way of achieving it:

let result = objArray.map(a => a.foo);

OR

let result = objArray.map(({ foo }) => foo)

Extract array from object JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    var arr = [{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
    {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
    {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
    {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
    {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}];

    var cuisines = arr.map(function(el) {
      return el.cuisineType;
    });

  console.log(cuisines); // array
  console.log(cuisines.join(', ')); // formatted string
</script>

</body>
</html> 

Output:

Extract array from object JavaScript

Extract Given Property Values from Objects as Array

Using a map() you can it.

<script>
    const objArray = [{a: 1, b: 2}, {a: 4, b: 5}, {a: 8, b: 9}];
    let res = objArray.map(item => item['a']);
    console.log(res);
</script>

Do comment if you have any doubts or suggestions on this JS Array object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading