0

I need to add to second array objects, from first array (if they don't found in second array).

For example:

var array1 = [{username: user1, id: 11}, {username: user2, id:12}, {username: user3, id:13}, {username: user4, id:14}];
var array2 = [{username: user4, id:14}, {username: user43, id:43}];

var result array = [{username: user1, id: 11}, {username: user2, id:12}, {username: user3, id:13}, {username: user4, id:14}, {username: user43, id:43}];

I think, it need check by id;

Onur Topal
  • 3,042
  • 1
  • 24
  • 41
trigger
  • 489
  • 10
  • 26
  • http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – APD Apr 10 '15 at 13:03
  • the type of the contents of the array makes no difference, you just have to compare the objects by id. – APD Apr 10 '15 at 13:10

3 Answers3

-1

I think you can use underscore and first do the union of the two arrays and then call http://underscorejs.org/#uniq

hope it helps.

alfredoo
  • 100
  • 8
  • sorry for -1 one but OP did not ask for a new library implementation asking for JS or Angular solutions. Adding a while new library just for a simple problem. – Onur Topal Apr 10 '15 at 13:41
-1

Use the lodash library

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.js"></script>

Your script

var array1 = [{username: "user1", id: 11}, {username: "user2", id:12}, {username: "user3", id:13}, {username: "user4", id:14}];
var array2 = [{username: "user4", id:14}, {username: "user43", id:43}];
var result_array = _(array1).union(array2).unique(function(a,b){
    return a.id;
}).value();

This code puts all the elements together from the arrays and then the callback to unique returns a value that lodash uses to determine what unique means.

If you find out you also need username you could return

a.id + "somethinguniquehere" + a.username

plunker

Shanimal
  • 11,517
  • 7
  • 63
  • 76
  • sorry for -1 one but OP did not ask for a new library implementation asking for JS or Angular solutions. Adding a while new library just for a simple problem. – Onur Topal Apr 10 '15 at 13:41
  • @OnurTOPAL that's not really fair. Do people publish "simple problems" to the web or do they publish scalable applications that take advantage of commonly used utility libraries to optimize code and simplify development? Just as the OP "did not ask for a new library impl", he did not ask for a pure JS solution either. I see nothing wrong with providing answers that offer solutions using common libraries (such as jquery, lodash/underscore, moment, gsap etc.) Please reconsider your down vote. – Shanimal Jun 12 '15 at 17:59
-1

this cannot be solved simply by JS array.concat not because of it is array of object. As you need to remove duplicates first. So I suggest using filter as it is widely supported details are here. For older browsers implement polyfill block to your system. Alternatively you can also use findIndex and its polyfill.

var array1 = [...];
var array2 = [...];

for(var 1=0, len=array2.length; i<len; i++) {
   if (array1.filter(function(el) { el.id === array2[i].id }).lenght == 0) {
      array1.push(array2[i]);
   }
}
Onur Topal
  • 3,042
  • 1
  • 24
  • 41