I have an object including only one Array(17). I would like to get from this array specific value, ex for index = 4 -> 1523181939
.
How can I get it?
I am getting a console result by running:
this.temp = this.flights['states'];
console.log(this.temp);
console.log(typeof this.temp);
My object this.flights
is of the form
{time: 1523183848, states: Array(1)}
states: [Array(17)]
time: 1523183848
__proto__: Object
When I call this.flights['states']
I get:
[Array(17)]
Lastly when calling this.flights['states'][0][4]
I get an error:
ERROR TypeError: Cannot read property '0' of undefined
Starting flights object I am getting from Opensky-Network Api:
{
"time": 1523183840,
"states": [
[
"89906e",
"EVA857 ",
"Taiwan",
1523183838,
1523183839,
121.2966,
25.1178,
716.28,
false,
111.38,
50.06,
8.78,
null,
746.76,
null,
false,
0
]
]
}
Answer
Working code is below.
Access your element of array on an object like this this.temp = this.flights.states[0][4];
Do it like below:
var flights = {"time":1523183840,"states":[["89906e","EVA857 ","Taiwan",1523183838,1523183839,121.2966,25.1178,716.28,false,111.38,50.06,8.78,null,746.76,null,false,0]]};
this.temp = this.flights.states[0][4];
console.log(this.temp);
console.log(typeof this.temp);
Check out now. It is working code now as per your requirement.
No comments:
Post a Comment