JS: json数据分别写入数组里面
vara=[{"id":"1231","name":"中箱"},{"id":"9387","name":"小箱"},{"id":"68433","name":"中箱"},...
var a = [{"id":"1231","name":"中箱"},{"id":"9387","name":"小箱"},{"id":"68433","name":"中箱"},{"id":"1231","name":"小箱"},{"id":"43566","name":"冷柜"}];
共有5条josn数据 根据箱类型写入每个数组里面 然后循环每个数组分别显示中箱信息,小箱信息,冷柜信息 展开
共有5条josn数据 根据箱类型写入每个数组里面 然后循环每个数组分别显示中箱信息,小箱信息,冷柜信息 展开
1个回答
推荐于2016-11-08
展开全部
var arr = [{
"id": "1231",
"name": "中箱"
}, {
"id": "9387",
"name": "小箱"
}, {
"id": "68433",
"name": "中箱"
}, {
"id": "1231",
"name": "小箱"
}, {
"id": "43566",
"name": "冷柜"
}];
var result = {};
for (var i = 0; i < arr.length; i++) {
var ai = arr[i], ain = ai.name;
if (!result[ain]) {
result[ain] = [ai];
} else {
result[ain].push(ai);
}
}
console.log(result);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询