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

116
Vistas
How to prevent keep add string to the object in JavaScript?

When click on run button each time Mi string adding how to prevent Result : 1000MiMi Expected result multiple time click : 1000Mi

function App() {
      let memory = [{size:1000}]
      const handleSubmit = () =>{
        memory.map((item) => {
          const manju =  item.size + 'Mi'
          item.size = manju 
          return item
        })
        console.log(memory)
      }
      return (<div>
    <button onClick={handleSubmit}>Run</button>
        </div>)
    }
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

If your trying to prevent item.size from repeatedly adding 'Mi' onto the end, trying using the .endsWith() method.

const handleSubmit = () => {
    memory.map((item) => {
      if (!(item.size+"").endsWith("Mi")) {
          const manju =  item.size + 'Mi';
      }
      item.size = manju;
      return item;
    });
    console.log(memory);
  }
about 4 years ago · Santiago Trujillo Denunciar

0

your problem is here

memory.map((item) => {
          const manju =  item.size + 'Mi'
          item.size = manju 
          return item
        })

you are overwriting the value of item

instead you have to return a new value

like this

memory.map((item) => {
   return {size: item.size + 'Mi'}
})
about 4 years ago · Santiago Trujillo Denunciar

0

Since you're not using the result of map() for anything, you should use forEach() instead.

To prevent multiple Mi suffixes, check if it has already been added.

const handleSubmit = () => {
  memory.forEach((item) => {
    if (!(item.size + "").endsWith("Mi")) {
      item.size += 'Mi';
    }
  });
  console.log(memory);
}

const memory = [{size: 1000}]
handleSubmit();
memory.push({size: 500});
handleSubmit();

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