1

I have kind of this Array :

array = [
     {
      a:0, 
      b:{x:1,y:2} 
     },
     {
      c:0,
      b:{x:3,y:2}},
    ]

And I have a new object like this

{
 e:0, 
 b:{x:2,y:5}
}

I want to test whether the objects in array have the same nested b object or not, if so I'll push the new object there, if it already exists, I'll replace the whole object with the newer one. Any help how could I achieve that please (Lodash, ES6..), I'm using Typescript btw.

3
  • try to use extend in jquery - api.jquery.com/jquery.extend Commented Mar 31, 2017 at 9:41
  • I'm not using Jquery ^^, you can check the tags there Thanks anyway. Commented Mar 31, 2017 at 9:42
  • @Amol B Lande $.extend is for plain objects, not arrays Commented Mar 31, 2017 at 9:43

2 Answers 2

1

You can simply use Array.prototype.some() method with your array to test if there's any matching b object with the searched object, your code will be:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

This is a working snippet:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

array = [{
    a: 0,
    b: {
      x: 1,
      y: 2
    }
  },
  {
    c: 0,
    b: {
      x: 3,
      y: 2
    }
  },
];

var searched = {
  e: 0,
  b: {
    x: 2,
    y: 5
  }
};


//Check an existing userId
console.log(checkIfObjectExists(array, searched));

//Check a non existing userId
console.log(checkIfObjectExists(array, {
  a: 0,
  b: {
    x: 1,
    y: 2
  }
}));

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

4 Comments

Objects are compared by reference. So obj.b=== element.b always fails
I've edited the snippet and changed the searched sub-object so that it is the same sub-object of the first object in the array, and ran but it shows false ..
@hindmost well pointed out, I updtaed the answer to compare the String representation of objects.
Thank you for the smooth answer
1

The following IsExists() retun true if same object already exists and return false if same object not exists.

function IsExists(YourList,Object ) {
        var i;
        for (i = 0; i < YourList.length; i++) {
            if (YourList[i].c === Object .c && YourList[i].b === Object .b) {
                return true;
            }
        }

        return false;
    }

call you function like:

    var NewObject={
     e:0, 
     b:{x:2,y:5}
    }

if (!IsExists(array ,NewObject.b))
{
array .push(NewObject);
}

if the IsExists(array ,NewObject.b)return false then push this object into array

2 Comments

@abuabu try it.. this will helpful
Thank you very much, I'll through @chsdk 's answer , it looks smoother without loops , I cannot upvote yours since I do not have enough reputations..

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.