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

292
Vistas
How to save the name of object in array object?

I have an array containing 7 objects named is NewClass.

enter image description here

When I make some changes to the objects, the object has lost its original NewClass name.

Like that:

enter image description here

In arrray[6] lost NewClass name.

So is there any way I can keep the NewClass name for that object?

Here is my function to handle this array:

useEffect(() => {
console.log(layers);
if (selectedLayerIndex && layers.length > 0) {

  console.log(
    'x',
    layers.map((x) => {
      if (x.feature.properties.editLayerId === selectedLayerIndex) {
        console.log(x);

        return (x = {
          ...x,
          options: { ...x.options, color: 'cyan', fillColor: 'cyan' },
        });
      }
      return x;
    })
  );
  
}

}, [selectedLayerIndex]);

Thank you so much!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

by using the syntax x = {...x, option: {/* some changes to the class */} here, you change the class to a object, which cause a lost of your class name.

If you want to keep it as a class, please use instance.attribute instead, see the following example

class Demo {
    constructor(color){
        this.color = color;
    }
}

var array = [new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red')];

console.log('original array', array);
console.log('');

array[6] = {...array[6], color: 'blue'};
console.log(array); // array[6] have been change to a object
console.log('');

array[5].color = 'green';
console.log(array); // array[5] is still a class
console.log('');

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