Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!