0

I'm using AJAX to retrieve data via php. The resulting object available to Jquery looks like this:

Object {1234: "Martin", 4567: "Alf", 8512: "Symon"}

using the following I can get the key:

if ('4567' in staff)
    console.log('found')

How would I check if Alf exists ?

I've tried inArray, indexOf and various other examples, but I've not managed to get this working.

Thanks

3

1 Answer 1

2

Use JavaScript reflection as following:

var obj = {1234: "Martin", 4567: "Alf", 8512: "Symon"};

var find = function(input, target){
    var found;
    for (var prop in input) {
    if(input[prop] == target){
        found = prop;
    }
    };

    return found;
};

var found = find(obj, 'Alf');

if(found){
    alert(found);
}

https://jsfiddle.net/8oheqd3j/

Sign up to request clarification or add additional context in comments.

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.