I have the following arrays:
var main_arr = [[54.5,225.5], [45.25,15.54], [1.5, 22.3]];
var sec_arr = [[1.51, 22.35], [54.32,32.54], [4.56,45.45]];
I want to get the last element of the first array and find the closest one by values from the second array.
Then I want the found element to be pushed in the main array and removed from the secondary. I want it to repeat until there are no elements in the secondary array.
The result should look like this:
[[54.5,225.5], [45.25,15.54], [1.5, 22.3], [1.51, 22.35], [4.56,45.45], [54.32,32.54]]
How can I do it using pure JavasScript without any frameworks?
Edit:
I'm not sure if I explained it well enough so I'm gonna give you a simpler version. You have one main and one secondary array of numbers. You can't reorder anything in the main one.
var main = [1, 5, 4, 3, 7];
var secondary = [9, 3, 4, 0, 5];
I want to get the number that is the closest to the last element of the main array and push it to it. Then it should be repeated until there are no elements in the secondary array. The result should look like this:
var main = [1, 5, 4, 3, 7, 9, 5, 4, 3, 0];
var secondary = [];