0

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
3
  • in your example of current input, on one hand u have items[0] being Array(12) but in your "expanded form", you have items[0] as Array(1), can you please clear that up for me? which one is it really? Commented Mar 18, 2021 at 19:59
  • "Tried to use [...] items.flat() , but didn't work, it had no impact on my array of array" I cannot reproduce it Commented Mar 18, 2021 at 19:59
  • 1
    [ [{a:1}] , [{b:2}] ].flat() does turn to [{a:1}, {b:2}] so could you please show a clearer picture of what's actually going on? Commented Mar 18, 2021 at 20:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't maintain the structure of [ {12 objects}, {2 objects} ] rather returns [ {14 objects} ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.