Using lodash I'm wondering if there is a better way of doing this.
I've got an array object like below.
{
"ALFA ROMEO": {
"MITO": [{
"carData": "stuff"
}]
},
"AUDI": {
"A1": [{
"carData": "stuff"
},
{
"carData": "stuff"
}
],
"Q3": [{
"carData": "stuff"
}],
"A3": [{
"carData": "stuff"
},
{
"carData": "stuff"
},
{
"carData": "stuff"
}
]
}
}
I'm using _.forEach to iterate through the object to structure the output simular to this:
ALFA ROMEO - 1
MITO (1)
AUDI - 5
A1 (2)
Q3 (1)
A3 (2)
Using this I can get part way there:
_.forEach(jData, (val, key) => {
console.log(key)
_.forEach(val, (val, key) => {
console.log(' ' + key + ' (' + val.length + ')')
})
})
Which give me this:
ALFA ROMEO
MITO (1)
AUDI
A1 (2)
Q3 (1)
A3 (2)
My question is this:
a) Is there a better way of doing this without a double loop?
b) Is the only way to get a total for each make by totalling up the model counts, or is there a better way of doing this?
Thanks.