Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

mohammed mayat
mohammed mayat
4,228 Points

trouble putting object array into a variable

once you log external data to the console, how do you iterate and put object array into a variable so can you use it

i want to get the avg_signal put it in a variable so I can use it to change the position of the shape I created in html canvas https://lukenelson.co.uk/wifi-tracking/track.php?callback=eggs

2 Answers

Steven Parker
Steven Parker
216,012 Points

If you're retrieving this data in JSON format, the response should be an array. For example, you could log all the "avg_signal" values in the first element to the console this way:

for (var req of response[0].probe_requests)
  console.log(req.avg_signal);

you can use the eggs array, the array returning from the eggs function and run this

eggs.map(item => item.probe_requests)[0].reduce((result, currVal) => { result.push(currVal.avg_signal); return result; }, []);