Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array element with a dot in its name

I'm reading data from a database and loading it in a array. When I print it to console I get something like this:

 [
 {'person.name':'name1', total:1},
 {'person.name':'name2', total:100}
 ]

So I iterate through it with this code:

for(var i=0;i<arr.length;i++){
    console.log(arr[i].total);
}

I can access the total but how can I access 'person.name'?

like image 629
Hohenheimsenberg Avatar asked Jan 21 '26 18:01

Hohenheimsenberg


2 Answers

Try something like:

console.log(arr[i]['person.name']);
like image 159
Mike Christensen Avatar answered Jan 23 '26 08:01

Mike Christensen


access it like an array :

arr[i]['person.name'];

Here's more info on why/when to use this notation : JavaScript property access: dot notation vs. brackets?

like image 42
gion_13 Avatar answered Jan 23 '26 08:01

gion_13