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

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

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 Report

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 Report

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 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!