ShaneKm
ShaneKm

Reputation: 21368

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');
}

Upvotes: 10

Views: 22620

Answers (6)

freedev
freedev

Reputation: 30217

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

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/

Upvotes: 4

Kapilrc
Kapilrc

Reputation: 1548

var item = [{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}];

var newItem = item.filter(function(i) {
  return i.MachineID == 2;  //it will return an object where MachineID matches with 2
});

console.log(newItem);  // will print [{"MachineID":"2","SiteID":"20"}]

Upvotes: 0

ericsoco
ericsoco

Reputation: 26313

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) {
    // ...
}

Upvotes: 2

hvgotcodes
hvgotcodes

Reputation: 120318

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;
}

Upvotes: 5

VisioN
VisioN

Reputation: 145478

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!");
}

Upvotes: 13

user1726343
user1726343

Reputation:

You could use this condition:

if (arr.filter(function(v){return this.MachineID == 2;}).length > 0)

Upvotes: 2

Related Questions