I have an array of objects:
Object = {
1 : { name : bob , dinner : pizza },
2 : { name : john , dinner : sushi },
3 : { name : larry, dinner : hummus }
}
I want to be able to search the object/array for where the key is "dinner", and see if it matches "sushi".
I know jQuery has $.inArray, but it doesn’t seem to work on arrays of objects. Or maybe I’m wrong. indexOf also seems to only work on one array level.
Is there no function or existing code for this?
If you have an array such as
You can use the
filtermethod of an Array object:In newer JavaScript implementations you can use a function expression:
You can search for people who have
"dinner": "sushi"using amapor a
reduceI’m sure you are able to generalize this to arbitrary keys and values!