2

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?

enter image description here

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
        ]
    ]
}
19
  • 1
    your question seems unclear to me. Do you want to find a value in Array ? please explain properly. Commented Apr 8, 2018 at 10:18
  • 1
    this.flights.states[0][4] ?! Commented Apr 8, 2018 at 10:36
  • 2
    Post your complete Object flights!!!! Learn to be more precise in what your problem is. What you posted is not a valid Javascript datastructure. Commented Apr 8, 2018 at 10:46
  • 1
    Please go read How to Ask, and provide a minimal reproducible example. All these screenshots and snippets of code get us nowhere, you need to present something that actually makes the problem reproducible. Commented Apr 8, 2018 at 10:52
  • 2
    "this.flights.states is not valid. Should be used this.flights['states'] instead" - mate you're just proving that you don't know the first thing about this to begin with. Commented Apr 8, 2018 at 10:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I need to get access to the Array itself. The object is this.flights['states'] and it consists of Array, which is it's value.
@TomekM post your exact Array
@TomekM, {time: 1523183848, states: Array(1)} states: [Array(17)] time: 1523183848 __proto__: Object here: states: [Array(17)] array does not look like part of your object but states: Array(1). Please check your response again and share it.
I am still getting the same error: AppComponent.html:1 ERROR TypeError: Cannot read property '0' of undefined
How you are getting this flights array?
|
1

Obviously you're getting your flight data asynchronously, but your code is trying to access it before it is available.

I have added an example simulating asynchronous data fetching using window.setTimeout below. Hope that solves your problem.

let obj = {
  getData() {
    // this will fill your flights objects after 2 seconds
    setTimeout(() => {
      obj.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]]}
    }, 200)
  },
  flights: undefined
}

obj.getData() // will fetch the data and after 200 milliseconds obj.flights will be available

console.log(obj.flights) // but not now!! returns undefined

setTimeout(function() {
  console.log(obj.flights.states[0][4])
}, 500)

2 Comments

I share my code, because setTimeout doesnt work. codeshare.io/5wleMj The error now is : ERROR TypeError: Cannot read property 'states' of undefined
You refuse to understand your problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.