How to get values from array of objects in javascript

Digvijay

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.

Nick Parsons

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));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get all values from an array of objects with nested arrays of objects?

How to get distinct values from an array of objects in JavaScript?

How to group values from an array of objects by same values with javascript?

how to get a list of key values from array of objects - JavaScript

how to get unique values from array of objects with addition of numbers

How to get total of values in array of javascript objects

How to get the combination of array values from nested arrays in an array of objects

JavaScript: How can I get new array of values from array of objects?

How to get objects with unique values for specific keys from array of objects?

How to get values from one array of objects into another array of objects

Javascript: How to get a unique list of values assigned to the same property from an array of objects?

How to get distinct values from an array of objects which has an Array inside in on one key and count JavaScript?

JavaScript How to get values from an array of objects into array of arrays so that each sub-array groups values from like keys?

Javascript - Efficient way to get distinct values from an array of objects

javascript How to get an objects value from an array full of objects?

How to get elements from an array of Objects (JavaScript)

How to get the objects from an array in angular or javascript

How to get array of objects in javascript from an array of dictionaries in python?

How can we get an array of values for a particular key, from a sequence of objects inside an array using Javascript

How can I get keys with unique values in an array of objects? (JavaScript)

Get values from an array of objects in JavaScript

How to get values from an array, inside another array, with stdClass Objects?

How to get array of unique values from arrays within an array of objects?

How to get unique values from an array of JS objects?

Reactjs: How to get values from array of objects

How to get unique values from an array of objects in JavaScript, but filtered by certain value

How to get last objects from an array of the identical objects?(JavaScript)

How to get array of objects with no duplicate values by adding 1 using javascript

Get unique values corresponding to the keys from an array of objects in JavaScript