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

89
Vistas
How to get an object out of a useState array?

I have a table with a row that has a checkbox, and I have an object called an ellipse.

I need to make the ellipse object to enter the savedEllipse array (which uses useState) as soon as I click on the checkbox.

In the test I did, I see that the ellipse object did go into the array, but now I have to make it out of the array once I uncheck the checkbox, and I couldn't quite figure out how I could do that with an object type like the ellipse object I have here.

You can see that I did the same thing with setChekcedState, which makes sure that the name of the checkbox comes in and out of the checkedState array, and it works great, but when I tried to do it with the ellipse object the same way, it did not work.

Do you have an idea?

I found (here in StackOverflow) a few questions that are close to this, but none of them dealt with this specific problem, so don't get confused

Here's the code:

export default function App() {

const [checkedState, setChekcedState] = useState([]);

const [savedEllipse, setSavedEllipse] = useState([]);

const ellipse = {
  a: [12, 22],
  b: [22, 55],
  c: [42, 68]
};

const ellipseSet = (e) => {
        const { checked, name } = e.target;
        if (checked) {
            setChekcedState((oldState) => [...oldState, name]);
            setCheckedEllipse((oldState) => [...oldState, ellipse]);
        } else {
            setChekcedState((oldState) => oldState.filter((row) => row !== name));
        }
};

return (

<TableContainer>
      <Table aria-label="simple table">
        <TableBody >
            <TableRow>
              <TableCell padding="checkbox">
                <Checkbox
                  onChange={ellipseSet}
                  checked={checkedState.includes((row.id).toString())}
                  name={1}/>
              </TableCell>
            </TableRow>
          
        </TableBody>
      </Table>
    </TableContainer>
  );  
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

use these 2 functions to check the equality of the objects

 function isEqual(object1, object2) {
      return arrayEquals(object1.a,object2.a) && arrayEquals(object1.b,object2.b) && arrayEquals(object1.c,object2.c);
    }

function arrayEquals(a, b) {
    return Array.isArray(a) &&
        Array.isArray(b) &&
        a.length === b.length &&
        a.every((val, index) => val === b[index]);
}

Then we can set the state in ellipse list :)

setCheckedEllipse((oldState) => oldState.filter((row) => !isEqual(row,ellipse));
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