0

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.

My Medi
  • 3
  • 2
  • You want to zip your arrays (see [this question](https://stackoverflow.com/questions/22015684/how-do-i-zip-two-arrays-in-javascript)). With your goal in mind, however, that is an overly complicated approach, and you should use Robby's solution below. – ProgrammingLlama Nov 07 '22 at 02:03

1 Answers1

1

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);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156