How would I check in my array of objects, if a specific item exists (in my case MachineId with id 2)?
[{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}]
I tried this:
if (index instanceof machineIds.MachineID) {
alert('value is Array!');
} else {
alert('Not an array');
}
If you need to check if a property exists in a JavaScript object, then there are three common ways to do that. The hasOwnProperty () method will check if an object contains a direct property and will return true or false if it exists or not.
Let's suppose we have an array of objects like the following: If we wanted to check if, for example, the name property with a specific value exists in the objects array, we could do it in the following ways: Introduced in ES5, the some () method returns a boolean value.
So you have probably tried to do a includes ("VALUE") to check if an object contains a certain value. But as it turns out, objects don’t have that very convenient function. The common ways to check if a value exists in a Javascript object is to: Extract all the values from the object into an array, then use the includes () function to check.
The some () method returns true if the user is present in the array else it returns false. You can use the some () method to check if an object is in the array. 2. Array.find () The find () method is available since ES6 and is not supported in Internet Explorer.
In cross browser way you may use jQuery.grep()
method for it:
var item = $.grep(machineIds, function(item) {
return item.MachineID == index;
});
if (item.length) {
alert("value is Array!");
}
The simplest to understand solution is to loop over the array, and check each one.
var match;
for (var i = 0; i < yourArray.length; i++) {
if (yourArray[i].MachineId == 2)
match = yourArray[i];
}
Note if there is more than one matching item, this will return the last one. You can also dress this up in a function.
function findByMachineId(ary, value) {
var match;
for (var i = 0; i < ary.length; i++) {
if (ary[i].MachineId == value)
match = ary[i];
}
return match;
}
There are many standard solution, you don't need third party libraries or loop iteratively.
some
method - since JavaScript 1.6.find
method - since ES6findIndex
method - since ES6For example, using some()
;
var yourArray = [{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}];
var params = {searchedID: "2", elementFound: null};
var isCorrectMachineID = function(element) {
if (element.MachineID == this.searchedID);
return (this.elementFound = element);
return false;
};
var isFound = yourArray.some(isCorrectMachineID, params)
Array some
method accepts two parameters:
Callback function is not coupled with the iteration code and, using thisObject parameter, you can even return to the caller the element found or more data.
If such an element is found, some
immediately returns true
http://jsfiddle.net/gu8Wq/1/
Old question at this point, but here's an ES6 solution that uses Array.find:
let machine2 = machines.find((machine) => machine.id === '2');
if (machine2) {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With