I am working with an array of objects represented as follows.
data: [ {col: ['amb', 1, 2],} , {col: ['bfg', 3, 4], },]
My goal is to transform this data into an array of arrays like the one shown below.
[ [{a: 'amb',b: [1], c: 'red'}, {a: 'amb',b: [2], c: 'orange'}],
[{a: 'bfg',b: [3], c: 'red'}, {a: 'bfg',b: [4], c: 'orange'}]
]
I have attempted a solution outlined below.
let arrInner: Array<any> = []
let arrOuter: Array<Array<any>> = []
_.forEach(data, (item, i) => {
//create two objects redObj and orangeObj
redObj = {
a: item.col[0].toString(),
b: [item.col[1] as number],
c: 'red'
}
orangeObj = {
a: item.col[0].toString(),
b: [item.col[2] as number],
c: 'orange'
}
//put those two objects into an inner array
arrInner.push(redObj)
arrInner.push(orangeObj)
//assign the inner array to the outer array
arrOuter[i] = arrInner
})
However, when I check the values in arrOuter, it does not match my expected output. What could be the issue here and how can I correct it?