1 回答
TA贡献1848条经验 获得超2个赞
如果您想计算所有位置的所有汽车的总数,您可以做一个简单的操作,.reduce()从总数开始0,然后添加每个数组的长度autos。这是一个缩减数据集的示例:
const dataBase = [
{
"sucursal": "Quilmes",
"direccion": "Av. Hipólito Yrigoyen 80, B1878 Quilmes, Provincia de Buenos Aires",
"telefono": "(0230) 466-3000",
"autos": [
{
"marca": "chevrolet",
"modelo": "camaro",
"anio": 20120,
"color": "black"
},
{
"marca": "renault",
"modelo": "sandero",
"anio": 2020,
"color": "gray"
}
]
},
{
"sucursal": "San Miguel",
"direccion": "Av. Pres. Juan Domingo Perón 2043, B1663 San Miguel, Provincia de Buenos Aires",
"telefono": "(011) 4451-5565",
"autos": [
{
"marca": "nissan",
"modelo": "note",
"anio": 2018,
"color": "red"
}
]
}
];
const totalCount = dataBase.reduce((prev,curr) => {
return prev + curr.autos.length;
},0)
console.log(`There are ${totalCount} cars in total.`);
添加回答
举报