Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value exists in JavaScript object

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');
}
like image 246
ShaneKm Avatar asked Nov 29 '12 13:11

ShaneKm


People also ask

How to check if a property exists in a JavaScript Object?

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.

How to check if a specific value exists in an array?

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.

How to check if an object contains a certain 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.

How to check if an object is present in an array?

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.


4 Answers

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!");
}
like image 159
VisioN Avatar answered Oct 13 '22 02:10

VisioN


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;
}
like image 37
hvgotcodes Avatar answered Oct 13 '22 02:10

hvgotcodes


There are many standard solution, you don't need third party libraries or loop iteratively.

  • Array some method - since JavaScript 1.6.
  • Array find method - since ES6
  • Array findIndex method - since ES6

For 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 to test for each element.
  • thisObject - Object to use as this when executing callback.

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/

like image 4
freedev Avatar answered Oct 13 '22 01:10

freedev


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) {
    // ...
}
like image 2
ericsoco Avatar answered Oct 13 '22 03:10

ericsoco