Can someone please tell me the best way to check if an object within an array of objects has a type
of 11
?
Below is what I have but it will alert for every object in the array, can I check the whole array and get it to alert just the once at the end?
I've seen methods like grep but I've been trying and can't get it to work. I'm using jQuery.
var x;
for (x = 0; x < objects.length; x++) {
if (objects[x].type == 11) {
alert("exists");
} else {
alert("doesnt exist");
}
}
Can someone please tell me the best way to check if an object within an array of objects has a type
of 11
?
Below is what I have but it will alert for every object in the array, can I check the whole array and get it to alert just the once at the end?
I've seen methods like grep but I've been trying and can't get it to work. I'm using jQuery.
var x;
for (x = 0; x < objects.length; x++) {
if (objects[x].type == 11) {
alert("exists");
} else {
alert("doesnt exist");
}
}
Share
Improve this question
edited Feb 15, 2013 at 0:17
Nope
22.3k8 gold badges49 silver badges73 bronze badges
asked Feb 15, 2013 at 0:13
maomao
1,0773 gold badges24 silver badges44 bronze badges
3
- What's wrong with what you're using? – Brian Cray Commented Feb 15, 2013 at 0:15
- @BrianCray he/she is alerting every iteration – Popnoodles Commented Feb 15, 2013 at 0:15
-
2
I've seen methods like grep but I've been trying and can't get it to work.
This is how you would use grep:var existGrep = jQuery.grep(objects, function(item) { return(item.type === 11); });
I only added it to show you how you getgrep()
to work but off course any of the other answers are more suitable/efficient likeArray.some
or usingbreak
asgrep()
will iterate through the whole array. – Nope Commented Feb 15, 2013 at 1:14
5 Answers
Reset to default 4Best way is use Array.some:
var exists = objects.some(function(el) { return el.type === 11 });
In the link there is also a shim for the browser that doesn't support it.
Otherwise you can just iterate:
var exists = false;
for (var i = 0, el; !exists && (el = objects[i++]);)
exists = el.type === 11;
Once you have the exists
variable set, you can just do:
if (exists) {
// do something
}
Outside the loop, in both cases.
Your code should actually do it. If you're bothered that the loop will continue uselessly, you can abort it by calling break;
if(objects[x].type == 11){
alert("exists");
break;
}
Make it a function:
function hasObjWithType11(arr) {
var x;
for (x = 0; x < arr.length; x++) {
if(arr[x].type == 11){
return true;
}
}
return false;
}
alert(hasObjWithType11([{type:1}, {type:11}]); // alerts true
This will do it
var exists = false;
for (var x = 0; x < objects.length; x++) {
if(objects[x].type == 11){
exists = true;
break;
}
}
if(exists){
alert("exists");
}
You could make the searching code more reusable by wrapping it into a separate function. That way you can externalize the condition for easier reading:
function array_contains(a, fn)
{
for (i = 0, len = a.length; i < len; ++i) {
if (fn(a[i])) {
return true;
}
}
return false;
}
if (array_contains(objects, function(item) { return item.type == 11; })) {
alert('found');
}
You could also use Array.some()
:
if (objects.some(function(item) {
return item.type == 11;
})) {
alert('exists');
}
For IE < 9, refer to the MDN documentation for a mock version.