Skip to content
Advertisement

How do I map coordinates from one array of arrays to another, keeping the same order?

I have two arrays that look like:

[["Jenny", 4],["Jayden", 8]]
// and 
[[23.6778,-67.87],[87.8652,-9.97]].

The coordinates are in exactly the same position as I’d like them to be in the new array. I want it to ultimately end up like:

[["Jenny", 4, 23.6778,-67.87], ["Jayden", 8, 87.8652,-9.97]

Is there a way to map the coordinates to the arrays in the first array? I’ve tried a few things, but no luck.

Answer

You can use map function and spread operator for joining arrays:

let a = [["Jenny", 4],["Jayden", 8]]
let b = [[23.6778,-67.87],[87.8652,-9.97]]
let c = a.map((item, idx) => [...item, ...b[idx]])
Advertisement