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

144
Vistas
How can I store previous values instead of storing the value in array of objects in react state

I am storing the prev values in an array of objects, for example [{ActFollow: 'BlN'},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}] I want to store the key and values in an object like this {ActFollow: 'BlN',ActSendGift: 'BlY', ActSubscribe: 'BlY'}

  const [activityTypes, setActivityTypes] = useState<any>([]); // state
 .then((response: any) => {
            setActivityTypes((oldArray: any) => [
              ...oldArray,
              {[item.channelSettingTypeId]: response.settingValue},
            ]);
          });
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can reduce the array of objects into an object.

You can do it by spreading (...) the current object into the resultant object, as shown below:

const 
  arrOfObjs = [{ ActFollow: "BlN" }, { ActSendGift: "BlY" }, { ActSubscribe: "BlY" }],
  obj = arrOfObjs.reduce((res, o) => ({ ...res, ...o }), {});

console.log(obj);

You can also do it using Object.assign, as shown below:

const 
  arrOfObjs = [{ ActFollow: "BlN" }, { ActSendGift: "BlY" }, { ActSubscribe: "BlY" }],
  obj = arrOfObjs.reduce((res, o) => Object.assign(res, o), {});

console.log(obj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

How about this, if the nesting is only one level deep

const data = [{ActFollow: 'BlN',ActSendGift: 'BlY', ActSubscribe: 'BlY'}]

console.log([{...data[0],"hey" : "world"}])

about 4 years ago · Juan Pablo Isaza Denunciar

0

const items = [{ActFollow: 'BlN'},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}]

let object = {}
items.forEach(item=>{
  for (const [key, value] of Object.entries(item)) {
    object = {
      ...object,
      [key]: value
    }
  }
})
console.log(object)

You can use this simple idea in React also. Just hold on the default empty object in state and update the object.

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