Nerdy Sid
Nerdy Sid

Reputation: 332

How to access object property inside an nested array of object?

How can i access the property fm and lm which is inside an mobile array.

array=[
       {
        "name":"siddhesh",
        "mobile":[{"fm":"83******","lm":"78******"}]
       }
     ];

Upvotes: 0

Views: 155

Answers (2)

kind user
kind user

Reputation: 41913

In case if there's more than only one object inside your array, use following solution:

var array = [{"name":"siddhesh","mobile":[{"fm":"83******","lm":"78******"}]},{"name":"another","mobile":[{"fm":"23******","lm":"18******"}]}],
    res = [].concat(...array.map(v => v.mobile.map(Object.values)));

    console.log(res);

Upvotes: 0

Vicenç Gascó
Vicenç Gascó

Reputation: 1346

tl;dr array[0].mobile[0].fm

You can just use regular array and object accessors: - Having an array named arr = [], you can get any member of it by arr[index]. - Having an object named obj = {}, you can get any property of it by obj.propertyName.

Therefore:

// array
const a = array[0];
// object
const b = a.mobile;
// array
const c = b[0];
// object
const d = c.fm;

Upvotes: 2

Related Questions