我有對象有相似對象的嵌套數組的數組:合併對象的嵌套數組中的JavaScript
var cars = [
{
Plate: '112211',
Details:
[
{
Desc: 'Blah11 Blah Blah',
Miles: '111',
Color: 'blue'
},
{
Desc: 'Blah B22lah Blah',
Miles: '222',
Color: 'green'
},
],
Make:'Honda'
},
Plate: '223322',
Details:
[
{
Desc: 'Blah Blah B33lah',
Miles: '333',
Color: 'yellow'
},
{
Desc: 'Blah B44lah Blah',
Miles: '444',
Color: 'red'
}
],
Make:'GMC'
},
Plate: '334433',
Details:
[
{
Desc: 'Bl55ah Blah Blah',
Miles: '555',
Color: 'blue'
},
{
Desc: 'Blah B66lah Blah',
Miles: '666',
Color: 'pink'
},
],
Make:'Ford'
}
]
我試圖找到一種方法,把它變成一個新的數組,變平或合併的嵌套數組。我們的目標是將有這樣的:
[
{
Plate: '112211',
Desc: 'Blah11 Blah Blah',
Miles: '111',
Color: 'blue',
Make:'Honda'
},
{
Plate: '112211',
Desc: 'Blah B22lah Blah',
Miles: '222',
Color: 'green',
Make: 'Honda'
},
{
Plate: '223322',
Desc: 'Blah Blah B333lah',
Miles: '333',
Color: 'yellow'
Make: 'GMC'
},
{
Plate: '223322',
Desc: 'Blah Bl444ah B33lah',
Miles: '444',
Color: 'red',
Make: 'GMC'
},
{
Plate: '334433',
Desc: 'Bl555ah Blah B33lah',
Miles: '555',
Color: 'blue',
Make: 'Ford'
},
{
Plate: '334433',
Desc: 'Blah Bl666ah B33lah',
Miles: '666',
Color: 'pink',
Make: 'Ford'
},
]
lodash _.flatten和_.flattenDeep沒有做任何事情。我也試圖做一個_.chain和扁平化,以建立一個新的數組,而是拿出空
var actions = _.chain(cars).flatten("Details");
for (i = 0; i < cars.length; i++) {
actions.each(function(action) {
list.push(cars[i].Plate);
list.push(action);
list.push(cars[i].Make);