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

3
  • Check this. LiraNuna's answer will help you. Commented Jun 28, 2016 at 6:22
  • what should your function add do? Commented Jun 28, 2016 at 6: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? Commented Jun 28, 2016 at 6:28

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

Comments

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"

Comments

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;
});

2 Comments

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.)
ha u started again @nnnnnn
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);

Comments

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 😁

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.