10

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

6 Answers 6

13

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!");
}
Sign up to request clarification or add additional context in comments.

Comments

5

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

2 Comments

For the specified data, you should compare to "2" instead of 2.
Or just remove one of those = signs
4

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/

2 Comments

Tried in Chrome 46. Could not see it in prototype. Is this function really available with all browsers?
2

You could use this condition:

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

Comments

2

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

2 Comments

Keep in mind that this does NOT work in all of IE and in half of mobile browsers (as of november 2016): MDN Array.find browser compability
Sure. If you're using ES6, you should be using Babel (or some comparable transpiler.)
0
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"}]

1 Comment

please describe working of your code or use self explanatory comments!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.