This might get a bit confusing and I’m not sure if it’s possible, but I would appreciate any help.
I have the following arrays (the items and number of lists might change, this is just an example):
var list_1 = ["A - 2" , "E - 5" , "C - 7"];
var list_2 = ["D - 2" , "A - 2" , "E - 3"];
var list_3 = ["C - 1" , "E - 8" , "A - 7"];
My expected output is:
var final = ["A - 2" , "C - 1" , "D - 2" , "E - 3"];
What I’m trying to do:
I’m trying to figure out how to go through each array item, see if the letter in the beginning of the item exists in the previous array and if the number in the item is lower than the previous item, replace it in the ‘final’ list.
Any ideas, or is this not possible?
jQuery is acceptable
First, you merge the lists (third + second + first):
Create a map into which you’re going to put the lowest number for each letter:
Loop through all items splitting each item to the letter (
parts[0]) and the number (parts[1]).You get the currently lowest number for the given letter. If there is no number or the new number is lower than the currently lowest one, you update the map.
Finally, you convert the map to an array.