Skip to content

JavaScript get value by key in an array of objects | Example code

  • by

Use JavaScript bracket notation property with key and array index to get value by key in an array of objects.

arrayObj[0]['key']

JavaScript gets value by key in an array of objects

A simple example code has an array of objects, which contain an array of named objects, and I need to get the object value where the key is “name” in the first object.

Extract Specific Key’s Values From an Array of Objects.

<!DOCTYPE html>
<html>
<body>

  <script>

    var arrayObj = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
    ];

    var res = arrayObj[0]['name']
    console.log(res)


  </script>

</body>
</html> 

Output:

JavaScript get value by key in an array of objects

Extract all values from the array object of the key Using Array.prototype.map()

    const users = [
    { id: 0, name: 'John' },
    { id: 1, name: 'Wayne' },
    { id: 2, name: 'David' },
    ];

    const names = users.map(function (user) {
      return user.name;
    });

    console.log(names);

Output: [ “John”, “Wayne”, “David” ]

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