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

292
Views
How to get values from array of objects in javascript

I have an array of object from which I am trying to get values using map operator but I am getting the whole json objects all I want is just array of values. Below is my code:

const obj = [
             {
              a: {
                  b: 'Paul',
                 }
             },
              {  
                c: 'Byeeee',
              }
           ];

obj.map((val) => console.log(val));  

what I am getting is

{ a: { b: 'Paul' } }
{ c: 'Byeeee' }

What I want is:

['Paul','Byeeee']

Someone let me know how can I get the desired output.

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

0

You can do this recursively. You can first start off by grabbing the values of your object, and then loop through those using .flatMap(). If you encounter a value that is an object, you can recursively grab the values of that object by recalling your function. Otherwise, you can return the value. The advantage of using .flatMap() here is that when the recursive call returns an array, we don't end up with inner arrays, but rather the array gets flattened into one resulting array:

const obj = [{ a: { b: 'Paul', } }, { c: 'Byeeee', } ];

const getValues = (obj) => {
  return Object.values(obj).flatMap(val => Object(val) === val ? getValues(val) : val);
}
console.log(getValues(obj));

about 4 years ago · Juan Pablo Isaza Report

0

you can use the following solution.

  const data = [{ a: { b: 'Paul' } }, { c: 'Byeeee' }];

  const flatObjectValues = (obj, result) => {
  // recursive function to get object values
  const objValues = Object.values(obj);
  if (objValues?.length > 0) {
    objValues.map((v) => {
      if (typeof v === 'object' && !Array.isArray(v)) {
        flatObjectValues(v, result);
      } else {
        result.push(v);
      }
      return v;
    });
  }
};

const updatedData = [];
data.map((x) => flatObjectValues(x, updatedData));
console.log('updatedData: ', updatedData);
about 4 years ago · Juan Pablo Isaza Report

0

You can use recursion with array.reduce, like fellowing.

function getAllValues(objuct) {
  return objuct.reduce((acc, curr) => {
    if (typeof curr === 'object') {
      return [...acc, ...getAllValues(Object.values(curr))];
    }
    return [...acc, curr];
  }, []);
}
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!