简体   繁体   中英

Get value from javascript nested array

I am new in coding JavaScript. So far I know how to set and get values from a multi array, but with this one I cannot find the right way to do it.

I am trying to get the email value from this array:

__arr.push(['id' ,'12541']);
__arr.push(['tag', {"sub":false,"email":"email@email.com"}]);

I tried

JSON.parse(__ar.tag.email)
document.write(__ar[2][2])

Everything I tried so far I got either undefined or tag[object, object] .

What's the easiest way to get it?

The email property is located on the second element of the array (that is index 1 of the zero based indexed array). So, to access it, you also need to access the second object of the element (again index 1) and then .email is at your hand:

document.write(__arr[1][1].email);

so you have an array as __arr, the first element you are pushing is id which is an array second array is having your email id.

so you can access as shown below.

I hope this will solve your issue

 var __arr = []; __arr.push(['id' ,'12541']); __arr.push(['tag', {"sub":false,"email":"email@email.com"}]); console.log("email id =>", __arr[1][1].email)

Assuming that you only push those two values, your array looks like the following:

[
  ['id' ,'12541'],
  ['tag', {"sub":false,"email":"email@email.com"}]
]

Means, that when you access it using __arr.tag.email will result in an undefined error, because it's an array not an object.

Therefore what you could do is, if you don't know exactly the index:

var __arr = [];
__arr.push(['id' ,'12541']);
__arr.push(['tag', {"sub":false,"email":"email@email.com"}]);

for (var i = 0; i < __arr.length; i++){
 if(__arr[i][0] === 'tag'){
   console.log(__arr[i][1].email);
   break;
 }
}

A dynamic way to do it (with level of 2 nested levels).

Basically, I used two nested loops and aggregated the emails into a list.

 let __arr = [] __arr.push(['id' ,'12541']); __arr.push(['tag', {"sub":false,"email":"email@email.com"}]); __arr.push(['tag2', {"sub":false,"email":"2222@email.com"}]); const emails = __arr.reduce((res, items) => { items.forEach(elem => { if (elem.email) res.push(elem.email) }) return res },[]) console.log(emails) // [ 'email@email.com', '2222@email.com' ]

I hope you know array is starting from its index that's base value is 0. in your code there is no such element which is available in index 2.

document.write(__ar[2][2]) // 

I know lots of answer is given here, but i just want to tell you even you are pushing value in "__arr" ie an array of array. so every element is storing in its index value.

var __arr = [];
__arr.push(['id' ,'12541']);
__arr.push(['tag', {"sub":false,"email":"email@email.com"}]);

console.log(__arr[0]) //return you ["id", "12541"]
console.log(__arr[1]) //return you ["tag", {sub: false, email: "email@email.com"}]

again you can see inside of your "__arr" there is a an array element
console.log(__arr[0][0]) //return you "id"
console.log(__arr[0][1]) //return you "12541"

console.log(__arr[1][0]) //return you "tag"
console.log(__arr[1][1]) //return you {sub: false, email: "email@email.com"}

and here what you want i.e.
console.log(__arr[1][1].sub) //return you false
console.log(__arr[1][1].email) //return you "email@email.com"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM