Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Map fuction on JSON object not working in ReactJS

I am using react with typescript. I have one external JSON file from which I am getting data to create elements on my front end. I have one button also when the user enables that button then only elements should create otherwise do nothing but my map function is not working.

Here is my code:

import Button from '../Button/Button';
import { useState } from 'react';
import Box from './Box';

const jsonObj = require("./Helpers/dataList.json");

type JsonData = {
    index: number,
    x: number,
    y: number,
    width: number,
    height: number,
  }
  
  type JData = {
    index: number,
    indexData: JsonData[]
  }

 const updateIndex = () => {
            let cIndex: number = ...;
            setCurrentIndex(cIndex);
        }
    }

const Test = () => {
    const [currentIndex, setCurrentIndex] = useState(0);
    const [boxFlag, setBoxFlag] = useState(false);
    const [boxText, setBoxText] = useState("Show Box");

    const showBoxes = () => {
        if(!boxFlag){
            setBoxText("Disable Box");
        }
        else{
            setBoxText("Show Box");
        }
        setBoxFlag(!boxFlag);
    }

  return <div className='container'>
        <div>
        {boxFlag &&
                jsonObj.map((x: JData) => {
                    if(currentIndex === x.index){
                        x.indexData.map((y: IData) => {
                            <Box x={y.x} y={y.y} height={y.height} width={y.width}/>
                        })
                    }
                })
        }
      </div>
          <Button name={boxText} onClick={showBoxes}/>
  </div>;
};

export default Test;

and my JSON file:

[
  { "index": 8, "x": 1141, "y": 582, "width": 238, "height": 268 },
  { "index": 8, "x": 265, "y": 572, "width": 291, "height": 317},
  { "index": 8, "x": 1665, "y": 491, "width": 266, "height": 323 }
 
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to edit your code for displaying the Boxes like this:

{boxFlag &&
    jsonObj.filter((x: JData) => currentIndex === x.index).map((y: IData) => {
        return <Box key={`${y.x},${y.y}`} x={y.x} y={y.y} height={y.height} width={y.width}/>
    })
}

Note that I've added the key attribute to each Box (It must be unique).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda