Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to find an object with a certain property equals a certain value? [closed]

Tags:

jquery

Using jQuery, how do I iterate through an array of objects and return the one that meets certain criteria?

like image 949
David Avatar asked Mar 09 '12 00:03

David


People also ask

How do I check if an object has a property value?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.

How do you check if an array of object contains a value?

Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false.

How do you check if a value exists in an array of objects JavaScript?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.

How do you access the properties of an object?

You can access the properties of an object in JavaScript in 3 ways: Dot property accessor: object.property. Square brackets property access: object['property'] Object destructuring: const { property } = object.


2 Answers

$([1,2,2,4]).filter(function(i,n){return n==2;});

this will return both 2's

basically the array can be an array of dom elements or any array actually, if it is a array returned by a jQuery selector you can do some thing like

$('div.someClass').filter(function(){return $(this).hasClass('someOtherClass')})

just for eg-> this will return all divs which have both someClass and someOtherClass (note: there are other ways to do this)

updating as per your comment, you can do

$(yourArray).filter(function(i,n){return n.Amount && n.Amount == conditionValue;});
like image 116
labroo Avatar answered Sep 21 '22 06:09

labroo


You can use the jQuery grep function:

var matches = jQuery.grep(array, function(item) {
    // this is a reference to the element in the array
    // you can do any test on it you want
    // return true if you want it to be in the resulting matches array
    // return false if you don't want it to be in the resulting matches array

    // for example: to find objects with the Amount property set to a certain value
    return(item.Amount === 100);
});
// matches contains all objects that matches
if (matches.length) {
    // first match is in matches[0]
}

If your condition you want to test for is anything other than a strict equality, then you will have to use some sort of array iteration that executes your custom comparison. You could do it with .each() or with .grep() depending upon what type of output you want.

If you condition was a strict equality, you could use jQuery.inArray().

Obviously, you don't need jQuery for this as you could just iterate through the array yourself in plain javascript and implement whatever test you wanted. One advantage of using plain javascript is that you can break out of the iteration when you've found the result you want.

In regular javascript:

for (var i = 0, len = array.length; i < len; i++) {
    if (array[i].Price === 100) {
        // match is in array[i]
        break;
    }
}
like image 40
jfriend00 Avatar answered Sep 20 '22 06:09

jfriend00