Created
April 15, 2021 05:17
-
-
Save viebel/62b3375893bae993637e637f442e5a25 to your computer and use it in GitHub Desktop.
Joining arrays in JavaScript (like in SQL)
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
function joinArrays(a, b, keyA, keyB) { | |
var mapA = _.keyBy(a, keyA); | |
var mapB = _.keyBy(b, keyB); | |
var mapsMerged = _.merge(mapA, mapB); | |
return _.values(mapsMerged); | |
} | |
var dbBookInfos = [ | |
{ | |
"isbn": "978-1982137274", | |
"title": "7 Habits of Highly Effective People", | |
"available": true | |
}, | |
{ | |
"isbn": "978-0812981605", | |
"title": "The Power of Habit", | |
"available": false | |
} | |
]; | |
var openLibBookInfos = [ | |
{ | |
"isbn_13": "978-0812981605", | |
"title": "7 Habits of Highly Effective People", | |
"subtitle": "Powerful Lessons in Personal Change", | |
"number_of_pages": 432, | |
}, | |
{ | |
"isbn_13": "978-1982137274", | |
"title": "The Power of Habit", | |
"subtitle": "Why We Do What We Do in Life and Business", | |
} | |
]; | |
joinArrays(dbBookInfos, openLibBookInfos, "isbn", "isbn_13"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment