Home > Net >  Remove subarray of array and convert into objects?
Remove subarray of array and convert into objects?

Time:04-22

I have an array of arrays which I have to convert into an array of objects:

[
  [
    {
      lable: "text",
      anotherValue: "anothervalue"
    }
  ], 
  [
    {
      lable: "text",
      anotherValue: "anothervalue"
    }
  ]
]

I need to convert this into:

[
  {
    lable: "text",
    anotherValue: "another value"
  }, 
  {
    lable: "text",
    anotherValue: "another value"
  }
]

CodePudding user response:

You can do that using map. The function given to map as a parameter unpacks each subarray and replaces it by the object within.

a = [
  [{lable:'text',anotherValue:'anothervalue'}], 
  [{lable:'text',anotherValue:'anothervalue'}]
];

result = a.map((o) => o[0])
  • Related