0

I would like to merge my array into one while creating different objects inside it. The result I want to achieve is:

[{latitude: 19.04890787659077,longitude: 47.49627131324106}, {latitude: 19.07438856746581, longitude: 47.49834420053164}, {etc..}]

My current arrays look like this:

0: 19.04890787659077
1: 18.93150306374864
2: 18.570734077493597
3: 17.948338720442376
4: 18.594379058124062

0: 47.49627131324106
1: 47.44522085405608
2: 47.482922755413774
3: 47.31701880320728
4: 46.052752331223935

The data will be dynamic so I can't solve it by [{latitude: Arr[0], longitude: Arr[5]}, {etc..}]

1
  • 1
    Please show what you code you used to get that result, so we can point you in the right direction. Commented Jan 26, 2021 at 11:40

3 Answers 3

1

const arr1 = [
  19.04890787659077,
  18.93150306374864,
  18.570734077493597,
  17.948338720442376,
  18.594379058124062
];
const arr2 = [
  47.49627131324106,
  47.44522085405608,
  47.482922755413774,
  47.31701880320728,
  46.052752331223935
];

const result = arr1.map((v, i) => ({
  latitude: v,
  longitude: arr2[i]
}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

I am not 100% sure what you meant but I think you are looking for one of these:

Array.prototype.push.apply(arr1,arr2);

or

arr1 = arr1.concat(arr2)

These are the old school methods, the new way to do it is

var arr3 = [...arr1, ...arr2]

Comments

0

If you have only one array like this:

const a = [19, 18, 17, 18, 19, 47, 46, 46, 47, 47];

I tried this:

const mid = Math.floor(a.length/2);
const result = a.slice(0, mid).map((el, idx) => ({lat:el, lon:a[idx+mid]}))

Comments

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.