3

I have this code:

let data = res.data.data;

console.log('data: ', data)

const list = [];
for(let i = 0; i < data.length; i++){
    console.log('data i: ', data[i]) //not printing in console
    list.push({
        lat: data[i].latitude,
        lng: data[i].longitude,
        histories: data[i].histories,
    })

    lineString.pushPoint({
        lat:data[i].longitude, 
        lng:data[i].latitude
    })
}
console.log('list: ', list)

It return results as:

one

As you can see in my data part I have all results but in list part I get nothing!

Why I can't get my filtered data into list array?

2
  • Code looks pretty much correct to me. Maybe that's all executed asynchronously and data isn't set at that given point. A minimal example repo might help here. Commented May 14, 2020 at 7:00
  • @Aer0 A minimal example repo might help here what's repo? what should i do? Commented May 14, 2020 at 7:02

1 Answer 1

6

enter image description here

Well your data isnt an array, its an object so you need to loop over an object. The curly brace, blue marked on the image, indicates that its an object.

for(let prop in data){
   if(prop == "histories") continue;
   list.push({
      lat: data[prop].latitude,
      lng: data[prop].longitude,
      histories: data["histories"]
   })
}
Sign up to request clarification or add additional context in comments.

4 Comments

@mafortis please take a look i have updated it. its now cleaner
well data["histories"][prop] prop part is not needed (it returns error as there is no history prop in my data) and if condition also not needed i simply just pass some of my data into new array nothing about validating here :) and thank you for update
no the last line was ok, just [prop] part wasn't needed.
like this is ok: histories: data["histories"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.