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

138
Vistas
Making a value of an object to null in a object list in javascript

I have a list of object stored in a state called itemsToUpload. There's a button to upload the list of objects to a database. The following is an object in the list.

{
type:"Fiat",
 model:"500",
 color:"white",
imgUrl="https://latestlyhunt.com/wp-content/uploads/2021/03/Fiat-made-a-%E2%80%98Hey-Google-car.jpg"
};

When sending the data to the database, I want to make the imgUrl to null.

The following code is how I set the imgUrl to null.

const submitHandler =async()=>{

setitemsToUpload((items)=>
        items.map((item)=>({
          ...item,
          imgUrl:null
        })))

    const response =await axios.post("api url",itemsToUpload)
console.log(response )

}

After making the imgUrl to null,itemsToUpload state changes but the data sending to the API states that imgUrl is not null

What is the issue here?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Array.map doesn't modify the array, it creates a new array with the returns value, so you need to assign the result of your map to the array

const items = [
{
type:"Fiat",
 model:"500",
 color:"white",
imgUrl:"https://latestlyhunt.com/wp-content/uploads/2021/03/Fiat-made-a-%E2%80%98Hey-Google-car.jpg"
}
]


const modifiedItems = items.map((item)=>({
  ...item,
  imgUrl:null
}))

console.log(items)
console.log(modifiedItems)

about 4 years ago · Santiago Trujillo Denunciar

0

The code is working here. So I'm not sure if the problem belongs with this part.

const array = [{
type:"Fiat",
 model:"500",
 color:"white",
imgUrl:"https://latestlyhunt.com/wp-content/uploads/2021/03/Fiat-made-a-%E2%80%98Hey-Google-car.jpg"
},
{
type:"Fiat",
 model:"500",
 color:"white",
imgUrl:"https://latestlyhunt.com/wp-content/uploads/2021/03/Fiat-made-a-%E2%80%98Hey-Google-car.jpg"
},
{
type:"Fiat",
 model:"500",
 color:"white",
imgUrl:"https://latestlyhunt.com/wp-content/uploads/2021/03/Fiat-made-a-%E2%80%98Hey-Google-car.jpg"
}];

console.log(array);

const clearIt = array => {
  return array.map(item => {
  item.imgUrl = null
  return item;
  });
};

const newArray = clearIt(array);

console.log(newArray);

about 4 years ago · Santiago Trujillo Denunciar

0

In your code setitemsToUpload does not immediately mutates the itemsToUpload but rather tells React to schedule a state update and rerender, while submitHandler continues to run and calls axios.post before itemsToUpload is updated.

Try to refactor it like this:

const submitHandler = async () => {
    const newItemsToUpload = items.map((item) => ({
        ...item,
        imgUrl: null
    }))

    setitemsToUpload((items) => newItemsToUpload)

    const response = await axios.post("api url", newItemsToUpload)
    console.log(response)
}
about 4 years ago · Santiago Trujillo 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