0

I have two nested arrays.

arr1 = [["image1","shirt", "collared",40],["image3","shirt", "buttoned",40]]
arr2 = [["image1","blue"],["image2","red"]]

The desired output is : If the image names (image) match, I want to return the color from the second array to a variable.

I have tried using two for loops:

var color = for (var i = 0; i < arr1.length; i++ ) {
for (var j = 0; j < arr2.length; j++ ) {
if (arr1[i][0] === arr2[j][0]) {
return arr2[j][1]
}
}

Since this is a larger part of a program, The first loop gets executed much before the second loop...however both are nested into each other in the order that I have specified.....I am trying to using the variable to color some html elements, but my entire program gets halted. I am not sure if my approach is right.

8
  • 2
    you can not return from a loop and set a variable. Commented Feb 6, 2019 at 17:37
  • You missed a quote before blue: ["image1",blue"]. This should be a typo... Commented Feb 6, 2019 at 17:38
  • @FZs you can suggest an edit Commented Feb 6, 2019 at 17:39
  • @epascarello How could I return the color from the second arr? Commented Feb 6, 2019 at 17:39
  • @Adelin For one quote? Commented Feb 6, 2019 at 17:40

3 Answers 3

1

Feels like you're trying to use the second array as a lookup into the first. Here's a way to do this by transforming it into an object:

 function toLookupTable(shirtColors) {

     //keys will be image names, values will be colors
     const lookupTable = {};

     shirtColors.forEach(shirtColor => {
         //use array destructuring
         const [ image, color ] = shirtColor;
         lookupTable[image] = color;
     });
     return lookupTable;
 }

 const colorLookup = toLookupTable( [["image1","blue"],["image2","red"]] );

 console.log(colorLookup["image2"]); //outputs "red"
Sign up to request clarification or add additional context in comments.

2 Comments

personally I would have just used reduce.
Yeah, I find reduce can be quite hard-to-read and intimidating for novices. Could also have done it easily with lodash.
1

Use Array#reduce and Array#findIndex

I want to return the color from the second array to a variable.

const arr1 = [["image1","shirt", "collared",40],["image3","shirt", "buttoned",40]]
const arr2 = [["image1", "blue"],["image2","red"]]

const res = arr2.reduce((a,[image,color])=>{
   if(arr1.findIndex(([n])=>n===image) > -1) a.push(color);
   return a;
}, []);

console.log(res);

Comments

0

You can use reduce

let arr1 = [["image1","shirt", "collared",40],["image3","shirt", "buttoned",40]];
let arr2 = [["image1","blue"],["image2","red"]];

let op = arr1.reduce((out,inp,index)=>{
  if(arr2[index].includes(inp[0])){
    out.push(arr2[index][1])
  }
  return out
},[] )

console.log(op)

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.