0

I have defined this structure:

var game_list = { 
    1: {name:'Vampires hunter', description:'The best vampires game in the world'},
    2: {name:'Christmas vampires', description:'The best vampires game in the world'},
    3: {name:'Fruit hunter', description:'The best vampires game in the world'},
    4: {name:'The fruitis', description:'The best vampires game in the world'},
    5: {name:'james bond', description:'The best vampires game in the world'},
    6: {name:'Vampires hunter', description:'The best vampires game in the world'},
};

What I need to do (with plain js) is to create a function that find a string into that structure (the user can use small or caps), the func will have 2 parameters the array itself and the string to find.

Any help will be really appreciated.

2
  • This not multidimensional array but array of object. Would you like to find description from this structure? Commented Feb 21, 2014 at 16:42
  • @Suman you are right (a newbie here), i have changed the question. Commented Feb 22, 2014 at 11:07

1 Answer 1

1

Considering the structure of your data, it would make more sense to store the objects in an array, instead of nesting it inside an object like it is now.

That being said, you can use this function to find a string within the structure. It returns the object(s) which contains the string.

function findString(obj, str) {
    str = str.toLowerCase();
    var results = [];
    for(var elm in obj) {
        if(!obj.hasOwnProperty(elm)){
            continue;
        }
        for (var key in obj[elm]) {
            if (obj[elm].hasOwnProperty(key) && obj[elm][key].toString().toLowerCase().indexOf(str) > -1) {
                results.push(obj[elm]);
            }
        }
    }
    return results.length > 1 ? results : results[0];
}

See demo

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

11 Comments

thanks a lot @levi, thing is i can't change the structure since it´s im working within an already made code (work policies i guess), so im not pretty sure if your answer is forcing me to change the structure first, also i need to show the results.
@user3337697 I updated the answer to work with your existing structure. What you do with the results is a separate concern. I am logging the results simply to demonstrate the function.
im getting an error "Object1 has no method toLowerCase" in the console (Chrome) in this line "if (obj[elm].hasOwnProperty(key) && obj[elm][key].toLowerCase().indexOf(str) > -1)" Any idea of what's happening ? thanks a lot by the way.
im using the same structure and data as in the original question if that's what you mean.
weird. Do you get that error when viewing the demo http://jsfiddle.net/g5aq2/ ?
|

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.