0

I need to get the results back from the array. and that is my priority for now I need to get the results back from the array. and that is my priority for now

2 Answers2

0

foreach doesn't return a value it modifies the existing array. If you want to return a value you can use javascript Array.map() function

let x = [[{lat:"32",lng:"22"},{lat:"12",lng:"43"}],[{lat:"322",lng:"252"},{lat:"612",lng:"43"}]]; 

let res= x.flatMap(y => { return y.map(z => [z.lat, z.lng] ); }); 

console.log(res); 

Please check if this is how you meant the output to be?

Vipin
  • 424
  • 1
  • 5
  • 10
  • How I can nested two map inside another same as forEach here ? – Michael Dvios Mar 26 '21 at 08:52
  • so what is the output you are expecting? can you please share that – Vipin Mar 26 '21 at 08:55
  • I expect to return values like in last forEach [z.lat(), z.lng()] .. z.lat is latitude , z.lng is longitude. I expect [12.312312 , 43.342432] , [15.313412 , 34.342432] , [12.313412 , 22.342432] ... – Michael Dvios Mar 26 '21 at 08:57
  • let x = [[{lat:"32",lng:"22"},{lat:"12",lng:"43"}],[{lat:"322",lng:"252"},{lat:"612",lng:"43"}]]; let res= x.map(y => { return y.map(z => [z.lat, z.lng] ); }); console.log(res); Please check if this is how you meant the out put to be? – Vipin Mar 26 '21 at 09:06
  • This is no good result.. I have one more array inside. instead [12.312312 , 43.342432] , [15.313412 , 34.342432] , [12.313412 , 22.342432] i have [[12.312312 , 43.342432]] , [[15.313412 , 34.342432]] , [[12.313412 , 22.342432]] – Michael Dvios Mar 26 '21 at 09:15
  • Ok I have updated the answer check it – Vipin Mar 26 '21 at 11:05
0

forEach() — executes a provided function once for each array element. so the the forEach() method doesn’t actually return anything (undefined).

You can use map instead. map creates a new array with the results of calling a provided function on every element in the calling array.

credit to the article: https://codeburst.io/javascript-map-vs-foreach-f38111822c0f

Ran Turner
  • 14,906
  • 5
  • 47
  • 53