Aagam Doshi
Aagam Doshi

Reputation: 193

Convert an array of array of objects to array of objects?

Current Invalid Input

boxlist = [
 {
   boxId:'94sdsd65cc9'
   boxSize:'Small'
   boxType:'box'
   items:(2) [Array(12), Array(2)]
 }
]

Expanded above Items array of array sample:

items : [
  [{id: 1, name: "item1", write: true}],
  [{id: 3, name: "item3", write: true}]
]

Expected Output

boxlist = [
 {
   boxId:'94sdsd65cc9'
   boxSize:'Small'
   boxType:'box'
   items:(2) [{.12.}, {.2.}]
 }
]

Required Items array of objects sample:

items = [
  {id: 1, name: "item1", write: true},
  {id: 3, name: "item3", write: true}
]

Tried to use

  1. items.flat() , but didn't work, it had no impact on my array of array
  2. flatten, flattenDeep, flattenDepth for loadash but didn't work

Upvotes: 0

Views: 62

Answers (1)

boxlist.map((list) => {
    list.items = list.items.flat() // flattens the array of array
    return list
})

Upvotes: 1

Related Questions