Created
June 24, 2017 19:02
-
-
Save viniazvd/22da85e51e96469f53318015490f2833 to your computer and use it in GitHub Desktop.
concatenar, atualizar e ordenar arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const array1 = [ | |
[21, "Bowling Ball"], | |
[2, "Dirty Sock"], | |
[1, "Hair Pin"], | |
[5, "Microphone"] | |
] | |
const array2 = [ | |
[2, "Hair Pin"], | |
[3, "Half-Eaten Apple"], | |
[67, "Bowling Ball"], | |
[7, "Toothpaste"] | |
] | |
const merge = array1.concat( array2 ) | |
// const order = (a, b) => b[0] - a[0] | |
const update = (total, item) => { | |
total[item[1]] = total[item[1]] ? item[0] : item[0] | |
return total | |
} | |
const result = merge.reduce( update, {} ) | |
console.log( result ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
total[item[1]] = total[item[1]] ? total[item[1]] + item[0] : item[0]