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

95
Views
Getting last appearance of a deeply nested value by specified property

I've got this object as example:

const obj = {
    group: {
        data: {
            data: [
                {
                    id: null,
                    value: 'someValue',
                    data: 'someData'
                }
            ]
        }
    }
};

My goal is to get a value by propery name.

In this case I would like to get the value of the data propery, but the last appearance of it.

Meaning the expected output is:

someData

However, I'm using this recursive function to retreive it:

const findVal = (obj, propertyToExtract) => {
  if (obj && obj[propertyToExtract]) return obj[propertyToExtract];

  for (let key in obj) {
      if (typeof obj[key] === 'object') {
          const value = findVal(obj[key], propertyToExtract);
          if (value) return value;
      }
  }
  return false;
};

Which gives me the first appearance of data, meaning:

data: [
    {
       value: 'someValue',
       data: 'someData'
    }
]

How can I get the wanted result?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

one way can be to recursively flat the object and just get the wanted index (here data)

const obj = {
group: {
    data: {
        data: [
            {
                id: null,
                value: 'someValue',
                data: 'someData'
            }
        ]
    }
}
};

function deepFlat(obj) {
  let flatObj = {};
  flat(obj,flatObj);
  console.log(flatObj);
  return flatObj;
}

function flat(toFlat, flatObj) {
  Object.entries(toFlat).forEach(elem => {
    if (elem[1] && typeof elem[1] === 'object') {
     flat(elem[1], flatObj);
    } else {
      flatObj[elem[0]] = elem[1];
    }
  });
}

let result = deepFlat(obj);
console.log(result['data']);

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!