Home > front end >  how to print array of object in array of array (keys, values all are elements)
how to print array of object in array of array (keys, values all are elements)

Time:11-07

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.

CodePudding user response:

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

  • Related