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

247
Views
What is the best way to access key/values of a object array when I don't know them?

I have this array above and I need every property of it

let arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}]

How could I extract every single key/value of it, once in theory, I don't know any of them? I have already tried using object.keys but it returns the indexes of my array.

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

0

This should work

const arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}];


// to iterate over each element in the arry  
arr.forEach(a => {
    // To Iterate over each key in the element object
    Object.keys(a).forEach(k => {
      // to print the value of the 'k' key
      console.log(k + ' : ' + a[k]);
  })
})

about 4 years ago · Juan Pablo Isaza Report

0

1) You can use flatMap and Object.keys to get keys from an array of objects.

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const result = arr.flatMap((o) => Object.keys(o));
console.log(result);

2) To find all values in an array

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const values = arr.flatMap((o) => Object.values(o));
console.log(values);

3) If you want to find out all keys and values in an object

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const result = {
  keys: [],
  values: [],
};

for (let obj of arr) {
  Object.entries(obj).map(([k, v]) => {
    result.keys.push(k);
    result.values.push(v);
  });
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

If you want to collect all the keys and values of a nested array of objects, you can use Array.prototype.reduce and then collect the keys and values of the nested objects in separate nested arrays, using Object.keys() and Object.values() respectively:

const arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}];

const allKeysAndValues = arr.reduce((acc, cur) => {
  acc.keys.push(...Object.keys(cur));
  acc.values.push(...Object.values(cur));
  return acc;
}, { keys: [], values: [] });

console.log(allKeysAndValues);

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!