0

Hi I have an array of objects

[
       {
         outletId: 619734
         tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb21oo"
       },
       {
         outletId: 619755
         tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb24ty"   
       },
       {
         outletId: 619700
         tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb2qwe"  
       }
       // and so on...
]

Then I'm creating another object

[
       {
         outletId: 619734
         tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb21oo"
       }
]

And I want to find if the new created object matches any of the object in the array. I tried this with no luck

$.each(objCollection, function () {
      if (this === newObject) {
         alert("Already exist!!!");
      }
});

Any idea?

Thanks in advance

3
  • show me where you instanciate newObject Commented Jun 3, 2014 at 9:09
  • The === operator is only going to work if they're literally the same object. Not just have the same properties, but refer to the same memory address. You'll need a function that compares two objects that you can call. Commented Jun 3, 2014 at 9:09
  • yes ok you said it "Then I'm creating another object" it's a new object so it will never be the same. Make a function to compare the two objects like @AnthonyGrist said. Commented Jun 3, 2014 at 9:11

3 Answers 3

2

Try this:

var exists = array.some(function(obj){
    return obj.outletId == search.outletId && obj.tleaderId == search.tleaderId;
});

if(exists){
    alert("Already exist!!!");
}

This assumes the object you're looking for is stored in a search variable:

var search = {
    outletId: 619734
    tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb21oo"
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is not gonna work based on OP example cuz search object is inside an array so you need to write search[0].outletId and search[0].tleaderId
Actually you want array.some, not array.filter. Not only does it automatically give you a boolean result, it short circuits the for loop as soon as a match is found.
@Mauro74 IMHO this should have been the accepted answer
@Alnitak I need it to work in IE8 so the accepted answer I've picked is the one that works best for my project.
@Mauro74: I'd suggest using the polyfill provided in the MDN's documentation for IE8 compatibility.
|
1

If I've understood your question correctly, you want something like this DEMO ?

var array = [{
    outletId: 619734,
    tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb21oo"
}, {
    outletId: 619755,
    tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb24ty"
}, {
    outletId: 619700,
    tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb2qwe"
}
// and so on...
]

var newArray = [{
    outletId: 619734,
    tleaderId: "3f8be9bf-5920-4d3d-b915-50ca76cb21oo"
}];

function matchCase(array1,array2){

    var matchFound = false;

    for (var i = 0; i < array1.length; i++) {

        item = array1[i];

        if (item.outletId === array2[0].outletId && item.tleaderId === array2[0].tleaderId) {

            matchFound = true;
            break;

        }
    }

    return matchFound;

}

console.log(matchCase(array,newArray));

3 Comments

IMHO, that callback passing is horrible, and completely unnecessary, and doesn't provide sensible logic for the case when the entry isn't found. The code isn't async, so just have matchCase return a boolean.
... Why did you add a callback like that? o.O
@Cerbrus, Just did it the hard way, didn't think of the case that I can simply return the found item if any, and if nothing is found return "".
-1

linking: Check this http://jsfiddle.net/taN4Z/ //checking new array values within old one

    $.each(obj2, function (key1,val1) {
      $.each(obj1, function (key2,val2) {
        if(val1.outletId==val2.outletId){
           alert("Already exist!!!"+val1.outletId);
        }
     });     
   });

Here obj2 ( new array ) values are checked with the old array ( obj1 ) values .

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.