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

145
Vistas
replace array with new array in object javascript

I have an object with two arrays that contain alternative emails and numbers. I'm trying to delete items out of the array and set the new array in the old ones' place.

const handleDelteMail = (evt) => {
    let tempData: any = [];
    tempData = JSON.parse(JSON.stringify(userData.emailAlt));
    let filtered = tempData.filter((mail) => mail._id !== evt.currentTarget.value);

    console.log(filtered);

    setUserData({ ...userData, [userData.emailAlt]: filtered });
};

filtered holds the correct new array that I want to put into userData, but having no luck.

this is what my userData object looks like:

const initialValues = {
    dateCreated: Date.now(),
    firstName: "",
    lastName: "",
    countryCode: "",
    numberPrime: "",
    numberAlt: [],
    companyName: "",
    consultant: "",
    industry: "",
    whatsapp: false,
    acceptTerms: false,
    emailPrime: "",
    emailAlt: [],
    skype: "",
    userType: "",
    agent: "",
    comment: "",
};

I don't know what I'm missing or doing wrong. Please help.

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

0

  1. filter already returns a new array so you don't need to make a copy of userData.emailAlt - just iterate over it.

  2. Watch your spelling (handleDelteMail should probably be handleDeleteMail).

  3. You can reconstruct the new object by assigning the new array to emailAlt.

function handleDeleteMail(evt) {
  
  const { value } = evt.target;
  
  const filtered = userData.emailAt.filter(mail => {
    return mail._id !== value);
  });

  setUserData({ ...userData, emailAlt: filtered });

}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use function get in module lodash

Try this one:

const _ = require('lodash'); // install `lodash` first

const { value } = evt.currentTarget;
  
const filtered = userData.emailAt.filter(mail => {
  return mail._id !== value);
});

_.set(userData, 'emailAtl', filtered)
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