David Biga
David Biga

Reputation: 177

Find value within object - JQuery

I am having an issue with trying to see if a value matches within an array that contains one objects.

Here is what I am doing:

var found = $.inArray(opt.ifpo_width, selectedOptions) > -1;

Lets say opt.ifpo.width contains 650. selectedOptions contains an object with a value of 650 so I will want found to return 0 because it that means the value has been found.

Heres an example of console.log of selectedOptions:

[Object, Object]
0: Object
active: true
......
ifpo_width: "650" <-- value I am checking
ifpo_x: "153"
ifpo_y: "86"
shown: false
__proto__: Object
1: Object
active: true
ifpo_width: "650" <-- this other object should not be here because there is already a width of the same value.
ifpo_x: "140"
ifpo_y: "102"
.....

What are your suggestions and thoughts about how I can check this selectedOptions for the value being checked with opt.ifpo_width?

Upvotes: 0

Views: 67

Answers (1)

user3401335
user3401335

Reputation: 2405

if ifpo_width can change, you can use a function like that

    function search(property, arr, value) {
        var t;
        for (t = 0; t < arr.length; t++) {
            if (arr[t][property] == value)
                return true;
        }

        return  false;
    }

and call it with

search("ifpo_width", YourArray, selectedOptions)

otherwise, more simple

    function search(arr, value) {
        var t;
        for (t = 0; t < arr.length; t++) {
            if (arr[t].ifpo_width == value)
                return true;
        }

        return  false;
    }

Upvotes: 1

Related Questions