0

I am trying to use map() function to iterate over a 3D array in Reacts' render method, but somehow it is not working and webpack complaints that there is an unexpected token. Here is the full code of my class:

class ExpressionGraph extends Component {
render() {
  const offsetX = - window.innerWidth / 4;
  const offsetY = - window.innerHeight / 4;
const x = 20;
const y = 20;
console.log(test_data["data"]);
const data = test_data["data"];

return (
  <Stage width={window.innerWidth} height={window.innerHeight}>
    <Layer offsetX={offsetX} offsetY={offsetY}>

not working -------> data.map((typeArr, idx) => {

        console.log(typeArr);
        console.log(idx);
        typeArr.map((neuronArr, typeIdx) => {
          const numOfPoints = neuronArr[0].length;
          const circleColor = Konva.Util.getRandomColor();
          [...Array(numOfPoints)].map((_, pointIdx) => {
            const x = neuronArr[0][pointIdx];
            const y = neuronArr[1][pointIdx];
            console.log("x: " + x);
            console.log("y: " + y);
            console.log("circleColor: " + circleColor);
            return <ColoredCircle x={x} y={y} color={circleColor}/>
          })
        })
      })
    </Layer>
  </Stage>
);
}
}
7
  • Can you share the console logs as well. Commented Jan 11, 2018 at 19:57
  • 5
    You need to wrap it in {} brackets like {data.map((typeArr, idx) => { ...})}. But you have too much nested map so syntax is not the only problem you have Commented Jan 11, 2018 at 19:57
  • @Prakashsharma is right. Commented Jan 11, 2018 at 19:59
  • @Prakashsharma why several nested maps is considered a bad practice, if I understand you correctly? Commented Jan 11, 2018 at 20:03
  • @NikitaVlasenko I am not saying it is bad practice. I mean your code will not render array of ColoredCircle elements because of nested map. Commented Jan 11, 2018 at 20:08

1 Answer 1

1

Wrap your map statement within {}

   {data.map((typeArr, idx) => {

    console.log(typeArr);
    console.log(idx);
    typeArr.map((neuronArr, typeIdx) => {
      const numOfPoints = neuronArr[0].length;
      const circleColor = Konva.Util.getRandomColor();
      [...Array(numOfPoints)].map((_, pointIdx) => {
        const x = neuronArr[0][pointIdx];
        const y = neuronArr[1][pointIdx];
        console.log("x: " + x);
        console.log("y: " + y);
        console.log("circleColor: " + circleColor);
        return <ColoredCircle x={x} y={y} color={circleColor}/>
      })
    })
  })}
</Layer>
Sign up to request clarification or add additional context in comments.

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.