0

I have a simple question but still haven't found neither figured out out to solve this.

I have this array:

var charsAndTypes = [
    {'type': 'Power', 'characters': ['Bunny','Buffalo']},
    {'type': 'Magic', 'characters': ['Sheep','Dragon']},
    {'type': 'Sense', 'characters': ['Fox','Lion']},
    {'type': 'Charm', 'characters': ['Cat','Raccoon']}
];

I need to search for the "characters", like "Bunny", and then catch the "type" to save it to a var, tried .map(), .filter, foreach inArray, but stil didn't work has I wanted

Basicly I need a function like this in the end

function searchInArray ( array, string ){
    //search in characters what array contains that string
    //then return the array that has the value
}

var charType = searchInArray(charsAndTypes, "Bunny"); 

//then the output must be "Power" in this case
console.log(charType.type);

5 Answers 5

1

Just loop through the array, and check if the object's array has the value.

var charsAndTypes = [
    {'type': 'Power', 'characters': ['Bunny','Buffalo']},
    {'type': 'Magic', 'characters': ['Sheep','Dragon']},
    {'type': 'Sense', 'characters': ['Fox','Lion']},
    {'type': 'Charm', 'characters': ['Cat','Raccoon']}
];

function search (arr, char) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].characters.indexOf(char) > -1) {
      return arr[i].type;
    }
  }
}

console.log(search(charsAndTypes, 'Bunny'));

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

1 Comment

Ok, that was my problem, i was returning the whole array and then logging the typ that returned undefined, thanks a lot for the help :D
0

What you probably need is find. It works like filter but only returns one value from the array, instead of an array of matches.

function search(arr, char)
  return arr.find(function(charAndType){
    return ~charAndType.characters.indexOf(char);
  });
}

It's ES6 and probably isn't cross-browser yet. There's a polyfill in that page though.

Comments

0

One simple for loop. If it doesn't find it it will return undefined.

var charsAndTypes = [
    {'type': 'Power', 'characters': ['Bunny','Buffalo']},
    {'type': 'Magic', 'characters': ['Sheep','Dragon']},
    {'type': 'Sense', 'characters': ['Fox','Lion']},
    {'type': 'Charm', 'characters': ['Cat','Raccoon']}
];


function searchInArray ( array, string ){
    for(i=0;i<array.length;i++){
        if(array[i].characters.indexOf(string)>=0)
          return array[i];
    }
}

var charType = searchInArray(charsAndTypes, "Bunny"); 

//then the output must be "Power" in this case
console.log(charType.type);

Comments

0

This function will do the trick.

function searchInArray ( arr, string ){
    var found = false;
    $.each(arr, function(k,v) {
        if (v.characters.indexOf(string) != -1) {
            found = v;
            return false;
        }
    });
    return found;
}

Fiddle: https://jsfiddle.net/kywoxut6/

Comments

0
var charsAndTypes = [
    {'type': 'Power', 'characters': ['Bunny','Buffalo']},
    {'type': 'Magic', 'characters': ['Sheep','Dragon']},
    {'type': 'Sense', 'characters': ['Fox','Lion']},
    {'type': 'Charm', 'characters': ['Cat','Raccoon']}
];

var search = function (arr, char) {
    for (var index = 0; index < arr.length; index++) {
        if (arr[index].characters.indexOf(char) > -1) {
            return arr[index];
        }
    }
}

var charType = searchInArray(charsAndTypes, "Bunny");
console.log(charType.type);

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.