My Medi
My Medi

Reputation: 3

how to print array of object in array of array (keys, values all are elements)

I am a beginner in JavaScript, and working on array converting somethings below i mention here. first of all, i have an array which is looks like:

array1= [ { "name": "Poul", "age": "28", "status": "unmarried" }, { "name": "Rose", "age": "20", "status": "unmarried" } ] Now i want to convert this array of object like this array of array.

somethings likes:

array= [ [ "name", "Poul", "age", "28", "status", "unmarried" ], [ "name", "Rose", "age", "20", "status", "married" ] ]

anyone can help me to convert array1 to array same as i want?

Thanks for your trying and helping in advance!

i tried push, concat to make this out.

first

const arr1 = array1.map(object => (Object.values(object))); const arr2 = array1.map(object => (Object.keys(object)));

enter image description here

now i want to add this two array of array in one array of array.

Upvotes: 0

Views: 50

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97130

You can use Object.entries() to obtain arrays of key/value pairs and flatten them with flat():

const data = [{
  "name": "Poul",
  "age": "28",
  "status": "unmarried"
}, {
  "name": "Rose",
  "age": "20",
  "status": "unmarried"
}];

const result = data.map(v => Object.entries(v).flat());

console.log(result);

Upvotes: 1

Related Questions