-1

Possible Duplicate:
How to find a value in a multidimensional object/array in Javascript?

I've these JavaScript objects in an array:

[{"id": 3}, {"id": 32}, {"id": 33}, {"id": 34}]

How can I perform an IF statement like this:

list = [3,32,33,34]
if x in list:
    "in the list!"
else:
    "not in the list"

How can I do this using jQuery/Javascript?

Any clues?


Edit:

This is a JSON output:

[{"id": 3}, {"id": 32}, {"id": 33}, {"id": 34}]
Community
  • 1
  • 1
André
  • 24,706
  • 43
  • 121
  • 178
  • What exactly are you trying to do? Searching for a number? – ATOzTOA Jan 09 '13 at 19:34
  • You say you have objects, but then in the example the "list" contains numbers, not objects. Which is it? – Pointy Jan 09 '13 at 19:36
  • @JanDvorak No. That's wrong and won't work. – Chris Jan 09 '13 at 19:37
  • I'm trying to get a solution to the case: If x is in the list, do this, else, do other action. – André Jan 09 '13 at 19:37
  • @Pointy I think the OP's point is that he wants to be able achieve the same functionality that `in` offers with arrays. – Chris Jan 09 '13 at 19:37
  • 1
    He's obviously coming from a Python background. Don't downvote or vote to close, this is a legitimate question. And no, indexOf works for arrays but not searching arrays containing objects. – Geuis Jan 09 '13 at 19:37
  • 1
    @Abody97 I can't tell what the OP is asking because the question is so unclear. – Pointy Jan 09 '13 at 19:38
  • @Geuis note that the close votes are for "exact duplicate" – John Dvorak Jan 09 '13 at 19:40
  • I edited the question. I think it *should* be clearer now. – Chris Jan 09 '13 at 19:41
  • Here's a way that involves filtering out the pesky objectnesssssssss of your objects. var source = [{"id": 3}, {"id": 32}, {"id": 33}, {"id": 34}]; var list = [3,32,33,34]; var newsource = JSON.stringify(source).replace(/[^\d\,]/gi,'').split(','); for(var i=0; i -1 ){ console.log('in the list'); }else{ console.log('not in the list'); } } – Geuis Jan 09 '13 at 20:03
  • newsource converts the object to a string, regex's out anything except numbers and commas, then splits on the commas to turn it back into an array. The "numbers" are still strings, so in your indexOf check you have to convert list[i] to a string too. **This is a really bad idea, by the way.** – Geuis Jan 09 '13 at 20:03
  • If you were in a situation where you had an array of objects containing many thousands of indexes and you were going to be doing many thousands of these searches, it would be wise to loop over the objects once and push their results into a separate array. Then you just indexOf on that array to see if the values exist or not. Much faster and more efficient this way and eliminates the need to constantly loop over the entire array. – Geuis Jan 09 '13 at 20:04

1 Answers1

1

Note: there is nothing such as JSON objects. Those are JavaScript objects. JSON is a subset of JavaScript.

If the layout of the object is the way you exemplified it:

function hasVal(obj, val) {
    for(var i = 0, len = obj.length; i < len; i ++) {
        for(var key in obj[i]) {
             if(obj[i][key] == val) return true;
        }
    }
    return false;
}

Then call it as such:

var obj = [{"id": 3}, {"id": 32}, {"id": 33}, {"id": 34}];
hasVal(obj, 32); //returns true
hasVal(obj, 10); //returns false
Chris
  • 26,544
  • 5
  • 58
  • 71