Timeline for How to merge two arrays in JavaScript and de-duplicate items
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 23, 2020 at 13:25 | comment | added | AbhimanuSharma |
Can we use it in associative array? let a = [{id: 1, name:"sam", id:2, name: "roy" }]; let b = [{id: 1, name:"sam", id:3, name: "john" }] Output should be [{id: 1, name:"sam", id:2, name: "roy" , id:3, name: "john"}]
|
|
Sep 20, 2019 at 9:41 | history | edited | simo | CC BY-SA 4.0 |
edited body
|
Sep 20, 2019 at 6:26 | history | edited | simo | CC BY-SA 4.0 |
deleted 16 characters in body
|
S Sep 19, 2019 at 23:02 | history | suggested | dota2pro | CC BY-SA 4.0 |
Converting Code to a Demo
|
Sep 19, 2019 at 21:34 | review | Suggested edits | |||
S Sep 19, 2019 at 23:02 | |||||
Mar 20, 2018 at 17:14 | comment | added | jinglesthula | @Andrew I suspect that the 2nd approach mentioned will become widely used since if it's understood what Sets are, it is actually pretty readable and not too "clever". The 1st method takes a bit of reading. | |
Jun 9, 2017 at 17:55 | comment | added | Andrew | @7vujy0f0hy If more obscure and shorter is better then maybe? | |
Apr 8, 2017 at 17:49 | comment | added | 7vujy0f0hy |
@Andrew: Even better: 1. var c = [...a, ...b.filter(o => !~a.indexOf(o))]; 2. var c = [...new Set([...a, ...b])]; ☺
|
|
Nov 21, 2016 at 9:55 | comment | added | pmrotule | Just a reminder of the current browser usage caniuse.com/usage-table for people anxious about IE6. | |
Nov 10, 2016 at 10:49 | comment | added | awe |
If I want to actually change a to add b , will it then be better to loop through and use push? a.forEach(function(item){ if(a.indexOf(item)<0) a.push(item); });
|
|
Oct 18, 2016 at 22:37 | comment | added | Tobi G. | You could merge different for IE6-support: c = Array.from(new Set(c)); | |
Sep 26, 2015 at 21:17 | comment | added | theGecko | The original solution here has the benefit of removing dupes within each source array. I guess it depends on your context which you would use. | |
Mar 2, 2015 at 22:25 | history | edited | simo | CC BY-SA 3.0 |
Update the answer as suggested by comment
|
Apr 15, 2014 at 10:08 | history | answered | simo | CC BY-SA 3.0 |