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
asked Jun 3, 2014 at 9:05
Mauro74
4,82615 gold badges62 silver badges83 bronze badges
3 Answers 3
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"
}
answered Jun 3, 2014 at 9:09
Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Batu.Khan
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].tleaderIdAlnitak
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.Alnitak
@Mauro74 IMHO this should have been the accepted answer
Mauro74
@Alnitak I need it to work in IE8 so the accepted answer I've picked is the one that works best for my project.
|
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));
answered Jun 3, 2014 at 9:17
W.D.
1,0411 gold badge8 silver badges13 bronze badges
3 Comments
Alnitak
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.Cerbrus
... Why did you add a callback like that? o.O
W.D.
@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 "".
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
lang-js
===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.