-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_loop.js
More file actions
38 lines (31 loc) · 1.29 KB
/
Copy pathjson_loop.js
File metadata and controls
38 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// I have a simple fix but of course you can refactor it to make life easy for you.
result = [
{ name: "Wild", lat: "47.9163329", long: "-97.0467415" },
[[[[[[[[[[[[[[[[[[[{ name: "Diamond", lat: "47.9163295", long: "-97.0495925" }]]]]]]]]]]]]]]]]]]],
[[{ name: "Gold", lat: "57.9163295", long: "-17.0495925" }]]
];
//this is just to hold the record from the loop
let loopFlag;
//the first loop from result
result.map(eachRecord => {
//set eachRecord to the flag
loopFlag = eachRecord;
//checks to make sure it is an object otherwise run another loop(if an array)
while (Array.isArray(loopFlag)) {
loopFlag.map(eachRecordInArray => {
loopFlag = eachRecordInArray;
});
}
// print keys
console.log('-------------------------------');
Object.keys(loopFlag).map(theKeys=>{
console.log(theKeys);
});
// print values
console.log('-------------------------------');
Object.values(loopFlag).map(theValues=>{
console.log(theValues);
});
});
// In this case it will always check and find only the object you need to retrieve the data from.
// You can try to add more `[]` to the second index of the result and you will see that it will loop till it finds an object to retrieve the data you need.