2

I have a question regarding a function in Javascript:

I simply compare an input array to another and return something depending on the result, but for some reason, my comparison doesn't work...

here's my function:

const createArrayColor = (labels) => {
  const combi = [
    ["bronze","gold","new","silver"],
    ["bronze","gold","silver"],
    ["gold","new","silver"],
    ["bronze","gold","new"],
    ["bronze","new","silver"],
    ["gold","silver"],
    ["bronze","gold"],
    ["gold","new"],
    ["bronze","silver"],
    ["new","silver"],
    ["bronze","new"],
    ["gold"],
    ["silver"],
    ["bronze"],
    ["new"],
  ];

  const color = [
    ["#614E1A","#c49c48", "#f4f3f3", "#A5A49B"],
    ["#614E1A", "#c49c48", "#A5A49B"],
    ["#c49c48", "#f4f3f3", "#A5A49B"],
    ["#614E1A", "#c49c48", "#f4f3f3"],
    ["#614E1A", "#f4f3f3", "#A5A49B"],
    ["#c49c48", "#A5A49B"],
    ["#614E1A", "#c49c48"],
    ["#c49c48", "#f4f3f3"],
    ["#614E1A", "#A5A49B"],
    ["#f4f3f3", "#A5A49B"],
    ["#614E1A", "#f4f3f3"],
    ["#c49c48"],
    ["#A5A49B"],
    ["#614E1A"],
    ["#f4f3f3"],
  ];

  let i = 0;

  combi.forEach((c) => {
      console.log("Color: " + color[i]);
      console.log("Combi:  " + c + " ,type:  " + c.constructor.name );
      console.log("labels: " + labels + " ,type:  " + labels.constructor.name );
      console.log("Is it equal? " + (labels === c));

    if (labels === c) {
      return color[i];
    }
    i = i + 1;
  })
}

(btw if you know how to make a combination of an array in JS I'd like to know that too, I'm sure there's a better way to do it that what I did but I didn't find it)

So as you can see I compare labels with each element of combi, but it is always false...

Here are the results of the console.log

I think it's a small mistake I made somewhere but I can't find it...

Community
  • 1
  • 1
Caroline Mazé
  • 133
  • 2
  • 11
  • see [this question](https://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript) (I would flag this as a duplicate but i accidentally linked to a question that was not quite the same thing, and removed my flag, now it won't let me flag it as a dupe again) – Robin Zigmond Mar 06 '19 at 11:04
  • is there particular reason that you store these color and color name values in multidimensional arrays? – Nikola Kirincic Mar 06 '19 at 11:06
  • why wouldn't you use object with pair for key:value of label:color? – Nikola Kirincic Mar 06 '19 at 11:07
  • not clear what you want to do – brk Mar 06 '19 at 11:07

2 Answers2

1

"c" and "labels" are two different arrays with exact content. See this question to get to know how to compare arrays.

EtK
  • 86
  • 6
0

If you need to keep forEach iterator for somewhat reason, you need to iterate twice through this array, since it is multidimensional, of 2 depths.

This would find first occurrence for given value:

const createArrayColor = (labels) => {
    const combi = [
        ["bronze","gold","new","silver"],
        ["bronze","gold","silver"],
        ["gold","new","silver"],
        ["bronze","gold","new"],
        ["bronze","new","silver"],
        ["gold","silver"],
        ["bronze","gold"],
        ["gold","new"],
        ["bronze","silver"],
        ["new","silver"],
        ["bronze","new"],
        ["gold"],
        ["silver"],
        ["bronze"],
        ["new"],
    ];

    const color = [
        ["#614E1A","#c49c48", "#f4f3f3", "#A5A49B"],
        ["#614E1A", "#c49c48", "#A5A49B"],
        ["#c49c48", "#f4f3f3", "#A5A49B"],
        ["#614E1A", "#c49c48", "#f4f3f3"],
        ["#614E1A", "#f4f3f3", "#A5A49B"],
        ["#c49c48", "#A5A49B"],
        ["#614E1A", "#c49c48"],
        ["#c49c48", "#f4f3f3"],
        ["#614E1A", "#A5A49B"],
        ["#f4f3f3", "#A5A49B"],
        ["#614E1A", "#f4f3f3"],
        ["#c49c48"],
        ["#A5A49B"],
        ["#614E1A"],
        ["#f4f3f3"],
    ];

    let i = 0;
    let foundColorValue;
    combi.forEach((combiArray, arrayIndex) => {
        combiArray.forEach( (combiLabel, labelIndex)=>{
              if(combiLabel === labels && foundColorValue === undefined){
                  
                 foundColorValue = color[arrayIndex][labelIndex];
                
              }
            

        });
        
      

    });
    return foundColorValue;

}

   let foundColor =  createArrayColor('silver');
   console.log('found ' + foundColor);
Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28