You set your response to true, don't break the loop and then set it to false again when you meet another value.
You may simply set initial condition and then break when you find an item. You can actually even not break when you find an item, but it is useless to continue iteration when already found an item.
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
var response = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i].status == "active") {
response = true;
break;
}
}
console.log(response);
Another elegant way is to use function and make a short-circuit return:
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
function hasActiveStatus(a) {
for (var i = 0; i < a.length; i++) {
if (a[i].status == "active") {
return true;
}
}
return false;
}
var response = hasActiveStatus(arr);
console.log(response);
Note that you had .len for some reason, I have replaced it with correct .length.
Even more elegant way is to use Array.prototype.some:
var arr = [{
"id": "1",
"status": "active"
}, {
"id": "2",
"status": "complete"
},
{
"id": "3",
"status": "complete"
}
];
var response = arr.some(function(x) { return x.status === "active"; });
console.log(response);
i<arr.length