2

I am puzzled.

  const oldPosts = state.items; // -> [{id: 12, ... }, {id: 13, ... }]
  const newPosts = action.posts // -> [{id: 12, ...}];
  // what I want to have is [{id: 12, ... }, {id: 13, ... }] / "merged" 
  const iWantMerged = ...??

So my approach would be something like this

  const merged = [...newPosts, ...oldPosts]; 
  // ->  [{id: 13, ... }, {id: 12, ... }, {id: 13, ... }] 
  // and then remove the duplicated items.

So two questions:

  1. Is this the right approach or is there better approach IN ES6?
  2. If no: what is the best way to remove the duplicates?
Ivan
  • 10,052
  • 12
  • 47
  • 78
Livioso
  • 1,242
  • 1
  • 11
  • 21
  • 1
    IMHO not a duplicate: The other question is for ES5... My questions is if there is a better way in ES6! – Livioso Feb 19 '16 at 14:41
  • 1
    This is not a duplicate of http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items since no answers are ES6 nor immutable. – Ivan Feb 19 '16 at 14:42
  • 1
    I don't think ES6 changes anything in the picture. Of course you could use spreading instead of `concat`, and maybe a `for of` loop for iteration, but the solutions stay the same as in the duplicate. – Bergi Feb 19 '16 at 14:44
  • @Ivan: None of the top four answers there mutate their input. Not sure what you're asking for about "immutable"? – Bergi Feb 19 '16 at 14:45
  • Also have a look at [this ES6 answer](http://stackoverflow.com/a/27664971/1048572) to the dupe question. – Bergi Feb 19 '16 at 14:50
  • 2
    None of the answers work with arrays objects though, not even the one you point to. http://jsbin.com/dokaqokafu/edit?js,console – Ivan Feb 19 '16 at 14:55
  • @Ivan: Because in your example, the arrays contain different objects. We don't know if, in the OPs case, the arrays contain different objects (with the same ID) or the same objects. This would have to b clarified anyway before anybody can give another answer. – Felix Kling Feb 19 '16 at 18:44
  • @FelixKling But that has nothing to do with this question being a duplicate or not. – Ivan Feb 19 '16 at 19:12
  • @Ivan: Well, if the objects are the same, then the answers in the duplicate will work. – Felix Kling Feb 19 '16 at 19:14
  • 1
    Then shouldn't that question have been asked first BEFORE marking as a duplicate? Immediately closing a question and marking it as a duplicate of another that does not actually answer the original question is not very inviting to new users or to the community as a whole. Perhaps this conversation should be moved to chat. – Ivan Feb 19 '16 at 19:21
  • @Ivan: We are all humans and make decisions based on the information we have. Sometimes we make false decision based on incomplete information. The OP can update and clarify their question and we can reopen it. No harm done. – Felix Kling Feb 22 '16 at 02:01
  • @user3774227: If your question is whether there is a better way in ES6 then the answer is: no. ES6 doesn't add any functionality that helps in this situation. *"what is the best way to remove the duplicates?"* Iterate over the array and keep track which IDs you have seen. – Felix Kling Feb 22 '16 at 02:02

0 Answers0