Mateusz Sorys
Mateusz Sorys

Reputation: 27

How to get some value from object array? JS

I have:

[Object, Object]

0:Object

val1 : "123"

name: "John"

1:Object:

val2 : "123"

name: "John"

How i can get values from this array object? I need name values from this two object.

Upvotes: 0

Views: 148

Answers (2)

Yosvel Quintero
Yosvel Quintero

Reputation: 19090

You can use Array.prototype.map() and directly return obj.name property:

const arr = [{val: "123", name: "John"}, {val: "123", name: "John"}];
const result = arr.map(obj => obj.name);

console.log(result);

Upvotes: 1

Mμ.
Mμ.

Reputation: 8552

As simple as the following code:

const array = [{val: "123", name: "John"}, {val: "123", name: "John"}];

const res = array.map(e => e.name);

console.log(res);

Upvotes: 0

Related Questions