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
        ]
    ]
}
Tomek
  • 53
  • 7
  • Is `this.temp` the object printed in the image you posted? – bugs Apr 08 '18 at 10:17
  • 1
    your question seems unclear to me. Do you want to `find` a value in Array ? please explain properly. – Shashank Vivek Apr 08 '18 at 10:18
  • 1
    `this.flights.states[0][4]` ?! – Jonas Wilms Apr 08 '18 at 10:36
  • I updated the description to show the problem – Tomek Apr 08 '18 at 10:43
  • `this.flights.states[4]` – connexo Apr 08 '18 at 10:45
  • 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.** – connexo Apr 08 '18 at 10:46
  • @connexo error occures: ERROR TypeError: Cannot read property '4' of undefined – Tomek Apr 08 '18 at 10:48
  • That means your object does not have a property `states`. – connexo Apr 08 '18 at 10:49
  • It seems from your error that `this.flights.states` is not defined (not sure why you're accessing it with bracket notation). What happens when you log `this.flights.states[0]`, hopefully the same error. Are you doing all the checking in the same spot in your code? – tenor528 Apr 08 '18 at 10:51
  • @connexo it has a property states, because it returns expected array. – Tomek Apr 08 '18 at 10:51
  • @tenor528 this.flights.states is not valid. Should be used this.flights['states'] instead – Tomek Apr 08 '18 at 10:52
  • 1
    Please go read [ask], and provide a [mcve]. All these screenshots and snippets of code get us nowhere, you need to present something that actually makes the problem reproducible. – CBroe Apr 08 '18 at 10:52
  • What ***exactly*** does your console.log argument look like? – connexo Apr 08 '18 at 10:53
  • 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. – CBroe Apr 08 '18 at 10:54
  • 1
    You got to be kidding. `foo.states[0][4]` works fine with the object you have just shown, https://jsfiddle.net/28psgz2L/ – CBroe Apr 08 '18 at 10:56
  • He's probably getting the data asynchronously, but trying to access it synchronously. That concept seems to be hard to grab for some people. – connexo Apr 08 '18 at 10:57
  • @TomekM, check my answer now. It is working. – Vikasdeep Singh Apr 08 '18 at 10:58
  • If connexio's suspicion/guess is correct, and you are getting this data via an async request, then you probably just need to go read https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call to learn how to handle this properly. – CBroe Apr 08 '18 at 11:02
  • @cbroe *Starting flights object I am getting from **Opensky-Network Api**.* – connexo Apr 08 '18 at 11:22

2 Answers2

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.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
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)
connexo
  • 53,704
  • 14
  • 91
  • 128
  • I share my code, because setTimeout doesnt work. https://codeshare.io/5wleMj The error now is : ERROR TypeError: Cannot read property 'states' of undefined – Tomek Apr 08 '18 at 11:52
  • You refuse to ***understand*** your problem. – connexo Apr 08 '18 at 12:08