0

I have two arrays of objects:

var objArray1 = [{'name':'abc', 'phone':'0333'}, 
                {'name':'xyz', 'phone':'0334'},
                {'name':'fgfh', 'phone':'0999'},
                {'name':'abc', 'phone':'0666'},
                {'name':'abc', 'phone':'0444'} 
               ]

var objArray2 = [{'name':'abc', 'mobile':'0333'}, 
                {'name':'xyz', 'mobile':'0334'},
                {'name':'fgfh', 'mobile':'0999'} 
               ]

I want to search index phone of objArray1 from objArray2.

If matches i want that object of objArray1 to be pushed in existing array, if no matches then push that object in joiners array.

Here is what i am trying to do.

objArray1.forEach(function(item){
    if (objArray2.indexOf(item.phone) < 0) {
        joiners.push(item)
    }else{
        existing.push(item)
    }
})

Above code is not working and putting all objects of objArray1 into joiners.

Current Result:

joiners = [{'name':'abc', 'phone':'0333'}, 
                {'name':'xyz', 'phone':'0334'},
                {'name':'fgfh', 'phone':'0999'},
                {'name':'abc', 'phone':'0666'},
                {'name':'abc', 'phone':'0444'} 
               ]

Wanted Result:

joiners = [{'name':'abc', 'phone':'0666'},
           {'name':'abc', 'phone':'0444'} 
          ]

And after that i want to check vise versa and make an array of leavers if any object of objArray2 do not exist in objArray1.

Noman Ali
  • 3,160
  • 10
  • 43
  • 77

2 Answers2

1

You can check existence using Array#some function. It returns true if the condition is satisfied, otherwise false. indexOf is checking by comparing the references, because you have different objects, you will always get false in the if statement.

I have changed a bit the code in the forEach function

const objArray1 = [
    {'name':'abc', 'phone':'0333'}, 
    {'name':'xyz', 'phone':'0334'},
    {'name':'fgfh', 'phone':'0999'},
    {'name':'abc', 'phone':'0666'},
    {'name':'abc', 'phone':'0444'}];

const objArray2 = [ 
    {'name':'abc', 'mobile':'0333'}, 
    {'name':'xyz', 'mobile':'0334'},
    {'name':'fgfh', 'mobile':'0999'}];
                
const joiners = [];
const existing = [];

objArray1.forEach(item => 
    objArray2.some(i => i.mobile === item.phone) ? 
    joiners.push(item) : existing.push(item));

console.log(joiners);
console.log(existing);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

You could take a hash table and use it as condition for pushing.

var objArray1 = [{ name: 'abc', phone: '0333' }, { name: 'xyz', phone: '0334' }, { name: 'fgfh', phone: '0999' }, { name: 'abc', phone: '0666' }, { name: 'abc', phone: '0444' }],
    objArray2 = [{ name: 'abc', mobile: '0333' }, { name: 'xyz', mobile: '0334' }, { name: 'fgfh', mobile: '0999' }],
    joiners = [],
    existing = [],
    hash = Object.create(null);

objArray2.forEach(function (item) { hash[item.mobile] = true; });

objArray1.forEach(function(item) {
    (hash[item.phone] ? existing : joiners).push(item);
});

console.log(joiners);
console.log(existing);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392