Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
merge json array objects based on ID
//JavaScript-C24.2.0 (SpiderMonkey) mergeArrayObjects([{"ASIN":12345,"Description":"Some description about ASIN 12345"}, {"ASIN":12347,"Title":"Title for ASIN 12347"}, {"ASIN":12346,"Title":"Title for ASIN 12346"}, {"ASIN":12347,"Description":"Some description about ASIN 12347"}, {"ASIN":12343,"Title":"Title for ASIN 12343"}, {"ASIN":12347,"Rank":"4"}, {"ASIN":12346,"Rank":"3"}, {"ASIN":12343,"Description":"Some description about ASIN 12343"}, {"ASIN":12345,"Title":"Title for ASIN 12345"}, {"ASIN":12348,"Description":"Some description about ASIN 12348"}, {"ASIN":12348,"Title":"Title for ASIN"},{"ASIN":12348,"Rank":"2"}, {"ASIN":12345,"Rank":"1"}, {"ASIN":12346,"Description":"Some description about ASIN 12346"}, {"ASIN":12344,"Description":"Some description about ASIN 12344"}, {"ASIN":12344,"Title":"Title for ASIN 12344"}, {"ASIN":12344,"Rank":"5"}, {"ASIN":12343,"Rank":"6"}]); function mergeArrayObjects(array) { let hash = new Map(); array.forEach((item)=> { var asin = item['ASIN']; var desc = item['Description']; var title = item['Title']; var rank = item['Rank']; let obj; if (hash.get(asin) != undefined) { obj = hash.get(asin); } else { obj = new Map(); } obj.set('ASIN', asin); if (desc != undefined) { obj.set('Description', desc); } if (title != undefined) { obj.set('Title', title); } if (rank != undefined) { obj.set('Rank', rank); } hash.set(asin, obj); }); let merged = []; for (let [key, value] of hash) { merged.push(value); } merged.forEach(item=>{ printMap(item); print('=================================='); }); } function printMap (map) { for (let [key, value] of map) { print(key + ' : ' + value); } }
run
|
edit
|
history
|
help
0
random3
Daddy
ROSHAMBO BITCHES!
Javascript reverse string, one index
ArraY_ROW addition
greeting
Set(), union, intersection, difference
Error
G
Futbal