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

161
Views
How to looping trough an array of object when array name is not consistent?

Consider below example:

const obj ={
price:[{multiple items},{multiple items}],
name:"",
id:"",
}

i want to apply foreach on price property but the issue is the property name is changing wrt cases like saleprice , taxprice etc.

if i am using obj.keys() method it doesn't work because it returns key as a string.

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

0

You can try this:

const obj = {
  price: [10, 20, 30],
  name: "",
  id: "",
};

const pricePropertyName = Object.keys(obj).find((prop) => prop.includes('price'));
if (pricePropertyName) {
  obj[pricePropertyName].forEach(el => {
    console.log(el);
  });
}
about 4 years ago · Juan Pablo Isaza Report

0

const key = Object.keys(obj).find((key) => key.indexOf('price') > -1)
obj[key].forEach(price => console.log(price))
about 4 years ago · Juan Pablo Isaza Report

0

You can use "Object.values()" or "Object.fromEntries()" to get the values or both keys & values.

const obj ={
price:[{multiple items},{multiple items}],
name:"",
id:"",
}

const priceArray = Object.values(obj).find( value => Array.isArray(value))

FYI

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

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!