3

Need to push the array object(arr1) valeus into another array object(arr2) if the value not exist. The existing values will not push into another array.

var arr1 = [{
    name: 'fred'
}, {
    name: 'bill'
}, {
    name: 'ted'
}, {
    name: 'james'
}];

var arr2 = [{
    name: 'spil'
}, {
    name: 'fred'
}, {
    name: 'bill'
},{
    name: 'paul'
}, {
    name: 'stone'
}];

function add(name) {
    var found = arr1.some(function (el) {
      return el.name === name;
    });
    if (!found) {
        arr1.push(arr2);
    }
    return arr2;
}

Fiddle

Raja O
  • 634
  • 1
  • 12
  • 33
  • Check [this.](http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items?answertab=active#tab-top) LiraNuna's answer will help you. – pkovzz Jun 28 '16 at 06:22
  • what should your function `add` do? – Nina Scholz Jun 28 '16 at 06:26
  • You are talking about pushing values into `arr2`, but your code does the opposite (or sort of tries to). Given that your `add()` function seems to take a string as input, what should happen (a) if the value is currently in neither array? (b) If it is currently in only `arr1`? (c) If it is currently in only `arr2`? (d) If it is already in both arrays? – nnnnnn Jun 28 '16 at 06:28

5 Answers5

6

You could use a hash table for look up, if the name property is already in arr1. If not push the actual item to arr1.

var arr1 = [{ name: 'fred' }, { name: 'bill' }, { name: 'ted' }, { name: 'james' }],
    arr2 = [{ name: 'toString' }, { name: 'spil' }, { name: 'fred' }, { name: 'bill' }, { name: 'paul' }, { name: 'stone' }],
    hash = Object.create(null);

arr1.forEach(function (a) {
    hash[a.name] = true;
});
arr2.forEach(function (a) {
    hash[a.name] || arr1.push(a);
});

console.log(arr1);

PS

Just to make clear, why Object.create(null), a really empty object as hash and not {}, an empty object. Pay attention to the item { name: 'toString' }. In the first part, the item gets inserted, in the second not, because hash has a property with the name toString.

var arr1 = [{ name: 'fred' }, { name: 'bill' }, { name: 'ted' }, { name: 'james' }],
    arr2 = [{ name: 'toString' }, { name: 'spil' }, { name: 'fred' }, { name: 'bill' }, { name: 'paul' }, { name: 'stone' }],
    hash = {}; // now an object

arr1.forEach(function (a) {
    hash[a.name] = true;
});
arr2.forEach(function (a) {
    hash[a.name] || arr1.push(a);
});

console.log(arr1);
Yennefer
  • 5,704
  • 7
  • 31
  • 44
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

If you're only appending a single variable, then push() works just fine. If you need to append another array, use concat(): var ar1 = [1, 2, 3]; var ar2 = [4, 5, 6];

var ar3 = ar1.concat(ar2);

alert(ar1);
alert(ar2);
alert(ar3);

Will spit out:

"1,2,3"
"4,5,6"
"1,2,3,4,5,6"

The concat does not affect ar1 and ar2 unless reassigned, for example:

ar1 = ar1.concat(ar2);
alert(ar1);

Will display:

"1,2,3,4,5,6"
1

Hi check this u can push the first array to other array later can use the underscore unique to return only the unique objects

   var arr1 = [{
    name: 'fred'
}, {
    name: 'bill'
}, {
    name: 'ted'
}, {
    name: 'james'
}];

var arr2 = [{
    name: 'spil'
}, {
    name: 'fred'
}, {
    name: 'bill'
}, {
    name: 'paul'
}, {
    name: 'stone'
}];

arr1.push(arr2);
arr1 = _.uniq(arr1, false, function(p) {
    return p.name;
});
Gayathri Mohan
  • 2,924
  • 4
  • 19
  • 25
  • And for the JS beginner who doesn't know what Underscore is? (Also, you're pushing the entire `arr2` array as a single element of `arr1` - I know the OP's code did this, but I don't think it's what they want.) – nnnnnn Jun 28 '16 at 06:31
  • ha u started again @nnnnnn – Gayathri Mohan Jun 28 '16 at 06:31
1

Use indexOf for get not exist name.Store first your arr1 name as a array !!!

var hasName = arr1.map(function(obj) { return obj.name; });
arr2.filter(function(v,i) { if(hasName.indexOf(v.name) == -1) arr1.push(v); });      
console.log(arr1);
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
1

If you have the luxury of using ECMAScript 6 then you can create this lovely block

var arr1 = [{
    name: 'fred'
}, {
    name: 'bill'
}, {
    name: 'ted'
}, {
    name: 'james'
}];

var arr2 = [{
    name: 'spil'
}, {
    name: 'fred'
}, {
    name: 'bill'
},{
    name: 'paul'
}, {
    name: 'stone'
}];

arr2.forEach(arr2Item => arr1.find(({ name }) => name === arr2Item.name) || arr1.push(arr2Item));

console.log(arr1);

Given that there's plenty of recursion, you may want to store your data in a different format so you can compare more easily. You could use object hash or ES6 Map to make your life easier. Also to save a few cycles

Grgur
  • 7,092
  • 2
  • 19
  • 34