javascript array look for similar object - if not present add to array

264
March 09, 2018, at 10:09 PM

I got an array of objects with structure like this

myArr = [
         {name: Test1, age: 20,  info: Test2}, 
         {name: Test2, age: 20, info: Test3},
         {name: Test5, age: 28, info: Test30}
 ]

With my custom action I am adding new info to array. But before adding new obejct into array I want to check if similiar one exist (same values for all keys). If yes - do nothing, if no similar object - add to array.

Simple for loop duplicates objects since it is running over all elements of the array.

var i;
for(i = 0; i < myArr.length; i++) {
    if((myArr[i].name === added.name) && (myArr[i].age === added.age) && (myArr[i].info === added.info) {
    }
    else {
        myArr.push(added);
    }
}
Answer 1

The problem is that you're adding within the loop. Instead, you want to add only after the loop if you didn't find a match.

So with minimal changes:

var i;
for(i = 0; i < myArr.length; i++) {
    if((myArr[i].name === added.name) && (myArr[i].age === added.age) && (myArr[i].info === added.info) {
        // Found, stop here
        break;
    }
}
if (i === myArr.length) {
    // Not found, add
    myArr.push(added);
}

But you can also use Array#find (added in ES2015, you can polyfill it for obsolete environments) or Array#some (also added in ES2015):

if (!myArr.find(function(e) { return e.name === added.name && e.age === added.age && e.info === added.info; })) {
    myArr.push(added);
}

It's a bit clearer if you use an ES2015 arrow function:

if (!myArr.find(e => e.name === added.name && e.age === added.age && e.info === added.info)) {
    myArr.push(added);
}
Answer 2

Check this. You can use .find() to match your condition and check if it is already present.

myArr = [ 
         {name: 'Test1', age: 20,  info: 'Test2'},  
         {name: 'Test2', age: 20, info: 'Test3'}, 
         {name: 'Test5', age: 28, info: 'Test30'} 
 ] 
  
let added = {name: 'Test6', age: 22, info: 'Test40'}; 
  
let matching = myArr.find(a => a.name === added.name && a.age === added.age && a.info === added.info); 
 
if(!matching) { 
  myArr.push(added) 
  console.log('added'); 
  console.log(myArr) 
}

READ ALSO
How call the same post on jQuery, there is a shortcut for this?

How call the same post on jQuery, there is a shortcut for this?

I need to starts the same ajax request when it finishesThe reason why I'm doing that, is because the setInterval does not supply me, there is no guarantees that the all requests made in the first second will be completed until the second setInterval call

143
How to show facebook like button, only if page isn&#39;t liked yet?

How to show facebook like button, only if page isn't liked yet?

Is there any way to hide the like button if my page is already liked?

160
Pass a blob to another page using ajax and php

Pass a blob to another page using ajax and php

I have a page that looks like this:

188
Suddenly some unknown PHP files are uploaded on my server [on hold]

Suddenly some unknown PHP files are uploaded on my server [on hold]

I have a website for classified used product uploadsWhich is build in PHP MySql And suddenly I've got some PHP files on my server

142