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

220
Views
Get values from dynamic object array

Hello In Javascript I get error result from API result array object can be

{
   ProjectName:["Invalid Project Name"],
   entityName:["Invalid entityName"]

}

or

{
   ProjectName:["Invalid Project Name"],
    
}

and key names are dynamic which comes from API. so I just need values as

["Invalid Project Name","Invalid entityName"]

How can I convert this object to this array ?

Thanks in advance

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

0

var errors = [];
var obj = {
  ProjectName: ["Invalid Project Name"],
  entityName: ["Invalid entityName"],
};
Object.keys(obj).map((k) => {
  obj[k].map((x) =>
    errors.push({
      key: k,
      message: x,
    })
  );
});

errors.map(k => {
  console.log("Check " + k.key + " field: " + k.message)
})

about 4 years ago · Juan Pablo Isaza Report

0

You can use Object.keys method with reduce

const obj = {
   ProjectName:["Invalid Project Name"],
   entityName:["Invalid entityName"]
}
const result = Object.keys(obj).reduce((prev, curr) => {
   prev.push(obj[curr][0]);
   return prev;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can use Object.values combined with map and flat:

var obj = {
  ProjectName: ["Invalid Project Name"],
  entityName: ["Invalid entityName"],
};
console.log(Object.values(obj).map(i => i).flat())

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!