我在Javascript中有这个数组:
cars = [
{color: red, type: sedan, name: ferrari},
{color: blue, type: sedan, name: ferrari},
{color: yellow, type: sedan, name: ferrari},
{color: red, type: coupe, name: toyota},
{color: red, type: sedan, name: toyota}
]
我想做的是创建一个多维数组,在其中我拥有第一个数组的信息,但是它看起来像这样:
我想将法拉利汽车与丰田分开
carsseparated[0] = [
{color: red, type: sedan, name: ferrari},
{color: blue, type: sedan, name: ferrari},
{color: yellow, type: sedan, name: ferrari}
]
carsseparated[1] = [
{color: red, type: coupe, name: toyota},
{color: red, type: sedan, name: toyota}
]
这就是我一直在编码的
$scope.carsseparated= [];
var cont = 0;
$.each($scope.cars, function (index,value) {
$scope.carsseparated[cont] = [value.color, value.type, value.name]
cont = cont+1;
}
答案 0 :(得分:1)
let cars = [{color: "red", type: "sedan", name: "ferrari"}, {color: "blue", type: "sedan", name: "ferrari"}, {color: "yellow", type: "sedan", name: "ferrari"}, {color: "red", type: "coupe", name: "toyota"},
{color: "red", type: "sedan", name: "toyota"}];
let car1 = cars.filter(val => val.name === 'ferrari');
let car2 = cars.filter(val => val.name === 'toyota');
console.log(car1)