Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a JavaScript object

Tags:

javascript

I have a JavaScript object like this:

[{
    name : "soccer",
    elems : [
        {name : "FC Barcelona"},
        {name : "Liverpool FC"}
    ]
},
{
    name : "basketball",
    elems : [
        {name : "Dallas Mavericks"}
    ]
}]

Now I want to search on this JavaScript object in the browser. The search for "FC" should give me something like this:

[
    {name : "FC Barcelona"},
    {name : "Liverpool FC"}
]

How to do this fast? Are there any JavaScript libs for this?

like image 311
Juri Glass Avatar asked Nov 30 '09 15:11

Juri Glass


People also ask

How do you find the element of an object?

find() The Array. find() method takes a callback function as parameter and executes that function once for each element present in the array, until it finds one where the function returns a true value. If the element is found it returns the value of the element, otherwise undefined is returned.

How do you search for a value or a key in an object?

To get an object's key by it's value: Call the Object. keys() method to get an array of the object's keys. Use the find() method to find the key that corresponds to the value. The find method will return the first key that satisfies the condition.


1 Answers

You might like using jLinq (personal project)

http://hugoware.net:4000/Projects/jLinq

Works like LINQ but for JSON and it allows you to extend it and modify it however you want to. There is already a bunch of prebuilt methods to check values and ranges.

like image 136
hugoware Avatar answered Oct 14 '22 11:10

hugoware