0

I have an object that includes different roles and I have an array that includes specific colors.

What I want to do, is see if the roles.name includes any of the strings in the colors array. I've tried different approaches, such as:

var colors = ["blue", "yellow", "brown", "red", "green", "pink", "purple"];
var roles = [{"name": "asd"},
            {"name": "blue"},
            {"name": "yellow"},
            {"name": "brown"}, 
            {"name": "red"},
            {"name": "green"},
            {"name": "pink"},
            {"name": "purple"},
            {"name": "fgh"},
            {"name": "jkl"}];
for (var i = 0; i < roles.length; i++) {
    if (roles[i].name.indexOf(colors)) {
   console.log(roles[i].name);
  }
}

And

var colors = ["blue", "yellow", "brown", "red", "green", "pink", "purple"];
var roles = [{"name": "asd"},
            {"name": "blue"},
            {"name": "yellow"},
            {"name": "brown"}, 
            {"name": "red"},
            {"name": "green"},
            {"name": "pink"},
            {"name": "purple"},
            {"name": "fgh"},
            {"name": "jkl"}];
roles.forEach(role => {
    if (role.name.indexOf(colors)) {
    console.log(role.name);
  }
});

But with both of the codes, the result is the full list of roles.

Expected result is all colors logged to the console. What I get is all roles logged to the console.

4
  • In ES6 you can use Array#find and Array#findIndex, there is also Array#includes Commented Sep 23, 2016 at 23:54
  • Your object has all the keys with the same name, literally, they're all called name, so you overwrite the previous keys and values. Commented Sep 23, 2016 at 23:54
  • How are all the roles being logged when you're treating an object like an array? Commented Sep 23, 2016 at 23:55
  • The var roles is incorrect and is not in fact like that. I made a mistake in that part. They're all actually separate objects. I edited my question. Commented Sep 23, 2016 at 23:56

4 Answers 4

3

You're using indexOf backwards. It should be array.indexOf(valueToSearchFor), but you have valueToSearchFor.indexOf(array). Also, indexOf returns the index, not a boolean; to test whether the element is found, you have to compare it to -1.

var colors = ["blue", "yellow", "brown", "red", "green", "pink", "purple"];
var roles = [{"name": "asd"},
        {"name": "blue"},
        {"name": "yellow"},
        {"name": "brown"}, 
        {"name": "red"},
        {"name": "green"},
        {"name": "pink"},
        {"name": "purple"},
        {"name": "fgh"},
        {"name": "jkl"}];
for (var i = 0; i < roles.length; i++) {
  if (colors.indexOf(roles[i].name) != -1) {
    console.log(roles[i].name);
  }
}

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

2 Comments

Ah, I actually tried this approach but without the != -1. Didn't know it wouldn't return a boolean. Thank you.
There's no excuse for that mistake. I'll bet every tutorial on using indexOf explains it.
0

var colors = ["blue", "yellow", "brown", "red", "green", "pink", "purple"];
var roles = [{"name": "asd"}, {"name": "blue"}, {"name": "yellow"}, {"name": "brown"},{"name": "red"}, {"name": "green"}, {"name": "pink"}, {"name": "purple"}, {"name": "fgh"}, {"name": "jkl"}];
var discovered = [];
for (var i = 0; i < roles.length; i++) {
   for (var j=0 ; j < colors.length ; j++){
     if (roles[i].name == colors[j]) {
        discovered.push(roles[i]);
     }
   }
}

console.log(discovered);

Comments

0

Your roles is only an Object. Prefer this format.

    let colors = [
    {
        "name": "blue"
    },
    {
        "name": "yellow"
    },
    {
        "name": "brown"
    },
    {
        "name": "red"
    },
    {
        "name": "green"
    },
    {
        "name": "pink"
    },
    {
        "name": "purple"
    }
]

And It's each color with using .map()

colors.map(function (color) {
    console.log(color.name)
})

Comments

0

You switched out role and colors when calling indexOf. And ´indexOf´ doesnt return a ´boolean´ but an integer and if it returns 0 it would evaluate to ´false ´to which will cause wrong results.

roles.forEach(function (role) {
  if (colors.indexOf(role.name) >= 0) {
    console.log(role.name);
  }
});

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.