0

This question have two parts: first, I want to create an array that works like a table. It will have two columns ids (Game-Id and Game-Name). I have been working in something like this:

var game_list = 
  [
    {id:1, name:'Vampires hunter'},
    {id:2, name:'Christmas vampires'},  
    {id:3, name:'Fruit hunter'},
    {id:4, name:'The fruitis'},
    {id:5, name:'james bond'},
    {id:6, name:'Vampires hunter'},
    {id:7, name:'Vampires avalon'},
    {id:8, name:'Vampires warrior'},
    {id:9, name:'Vampires hunter'}, 
    {id:10, name:'Vampires hunter'},
  ];

But I'm not able to access elements in this kind of array / object, not even a document.write of an element.

What I need is to create a function that will search for a specific string inside that array. The function will have 2 parameters (the string and the array) and it will give us a result an array of the elements with game names and ids that match that string.

1
  • Use Array.prototype.filter, with obj.name.indexOf(search) in your return statement. Commented Feb 21, 2014 at 15:02

2 Answers 2

2

Use filter:

function filter(arr, string) {
  return arr.filter(function (el) {
    return el.name === string;
  });
}

filter(game_list, 'Vampires avalon'); // [{ id=7, name="Vampires avalon"}]

Demo

If you want to be really clever, add some regex to match the string anywhere in the name:

function filter(arr, string) {
  var regex = new RegExp('.*' + string + '.*');
  return arr.filter(function (el) {
    return regex.exec(el.name);
  });
}

filter(game_list, 'hunter');

Which will give you this:

[{"id":1,"name":"Vampires hunter"},{"id":3,"name":"Fruit hunter"},{"id":6,"name":"Vampires hunter"},{"id":9,"name":"Vampires hunter"},{"id":10,"name":"Vampires hunter"}]

Demo

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

9 Comments

Nice, I like this better than mine!
thanks @Andy, it's not working with this: 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'} }; Im getting this error: return arr.filter(function (el) {
See here for more information on how to output stuff on the page.. Try not to use document.write, it's considered bad practice.
@Andy sorry to bother you but i was trying to make it work with the structure (in the prevoius comment) which is a bidimensional associative array, it will be REALLY appreciated.
I've updated the fiddle. There's an extra function called getData that takes your new structure and changes it into one that the code will understand.
|
0

A simple loop and check will do:

function checkArray(name) {
    var arr = [];
    for (var i = 0; i < game_list.length; i++) {
        if (game_list[i].name == name)
            arr.push(game_list[i])
    }
    return arr;
}

Will compare the name passed in with the name of each object, and returns an array of matching objects. If you only want names containing the passed in string, use indexOf and check on -1

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.