Skip to content

Instantly share code, notes, and snippets.

@viniazvd
Created June 24, 2017 19:02
Show Gist options
  • Save viniazvd/22da85e51e96469f53318015490f2833 to your computer and use it in GitHub Desktop.
Save viniazvd/22da85e51e96469f53318015490f2833 to your computer and use it in GitHub Desktop.
concatenar, atualizar e ordenar arrays
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 )
@viniazvd
Copy link
Author

total[item[1]] = total[item[1]] ? total[item[1]] + item[0] : item[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment