如何划分一个数组的信息并发送到另一个数组? [复制]
Posted
技术标签:
【中文标题】如何划分一个数组的信息并发送到另一个数组? [复制]【英文标题】:How to divide the information of an array and send it to another array? [duplicate] 【发布时间】:2019-04-26 05:11:11 【问题描述】:我在 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;
【问题讨论】:
【参考方案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)
【讨论】:
是的,我想这可能行得通,但我正试图让它动态化,我想说的是,我不想这样做:val.name === 'ferrari ' 谢谢你的回答。以上是关于如何划分一个数组的信息并发送到另一个数组? [复制]的主要内容,如果未能解决你的问题,请参考以下文章