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
Within an array of objects modify all previous objects to an object with a specific property

I have an array of objects, and according to a property inserted in one of them i would like to mark or select all the objects previous to that object container of the specific property

My array is in this way:

const arrX= [
      { status: '1' },
      { status: '2'},
      { status: '3', imHere: true },
      { status: '4' },
    ];

Then due to the property imHere on arrX[2], the positions arrX[0] and arrX[1] should be modified.

My expected result would be :

const arrX= [
      { status: '1',wasHere:true },
      { status: '2',wasHere:true},
      { status: '3', imHere: true },
      { status: '4' },
    ];

I know that the map method would be quite useful in this case, but can´t find the way to check from index of object containing imHere backwards the former positions

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

0

One approach is to use .findIndex() and .map():

const arrX= [{ status: '1' }, { status: '2'}, { status: '3', imHere: true }, { status: '4'}];

const imHereIndex = arrX.findIndex(({imHere}) => imHere === true);
const result = arrX.map((val, index) => index < imHereIndex
     ? { ...val, wasHere: true }
     : val
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Even if @Kinglish answer works like a charm I want to share another way to achieve your goal. This road is surely longer than Kinglish ones, never then less is a good alternative.

      { status: '4' },
    ];
    
function findProperty(arr) {
  const hasProperty = arr.findIndex(el => Object.keys(el).includes('imHere'))
  const addNewProperty = arr.map((el,i) => (i < hasProperty) ? {...el, wasHere: true} : el)
  
  return addNewProperty
}

const updatedArray = findProperty(arrX)
console.log(updatedArray)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's one method for it using Array#reduce and a boolean to track whether we've encountered inHere

const arrX = [
  {status: '1'},
  {status: '2'},
  {status: '3',imHere: true},
  {status: '4'},
];

let found = false,
  updated = arrX.reduce((b, a) => {
    found = found || (a.hasOwnProperty('imHere') && a.imHere === true)
    if (!found) a.wasHere = true;
    return b.concat(a);
  }, [])

console.log(updated)

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