js json递归 50
<code>[{"store_id":"1","store_name":"窝啦小城","goods_id":"100018","goods_name":"100%桑蚕丝防...
<code>
[
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100018",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 白色 M",
"goods_price": "220.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100024",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 M",
"goods_price": "235.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100026",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 XL",
"goods_price": "238.00"
},
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50"
}
]
</code>
怎么样将上面的json变成
<code>
[
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100018",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 白色 M",
"goods_price": "220.00",
"list": [
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100024",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 M",
"goods_price": "235.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100026",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 XL",
"goods_price": "238.00"
}
]
},
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50",
"list": [
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50"
}
]
}
]
</code>
根据store_id 进行归类 展开
[
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100018",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 白色 M",
"goods_price": "220.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100024",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 M",
"goods_price": "235.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100026",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 XL",
"goods_price": "238.00"
},
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50"
}
]
</code>
怎么样将上面的json变成
<code>
[
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100018",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 白色 M",
"goods_price": "220.00",
"list": [
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100024",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 M",
"goods_price": "235.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100026",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 XL",
"goods_price": "238.00"
}
]
},
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50",
"list": [
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50"
}
]
}
]
</code>
根据store_id 进行归类 展开
展开全部
根据你的需求,我写了个函数,并且测试通过。详细的代码如下,请自行参考并修改。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var a = [
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100018",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 白色 M",
"goods_price": "220.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100024",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 M",
"goods_price": "235.00"
},
{
"store_id": "1",
"store_name": "窝啦小城",
"goods_id": "100026",
"goods_name": "100%桑蚕丝防晒短披肩小外套空调衫薄外搭针织开衫女2015夏薄开衫 黄色 XL",
"goods_price": "238.00"
},
{
"store_id": "2",
"store_name": "菜场",
"goods_id": "100027",
"goods_name": "精选新鲜黄秋葵200g 新鲜蔬菜 16:00截单",
"goods_price": "7.50"
}
]
//这里是测试输出
console.log(encodeJson(a))
function encodeJson (a) {
//用来存放新的数组
var b = [];
//用来存放store_id,方便查找
var c = [];
var i=0,j=0,k=-1;
for(;i<a.length;i++){
var id = a[i]['store_id'];
for(j=0;j<c.length;j++){
if(c[j]==id){
k = j;
break;
}
}
if(k!=-1){
b[k].list.push(a[i])
}else{
b.push(a[i]);
var len = b.length;
b[len-1].list = [];
c.push(id);
}
k=-1;
}
return b;
}
</script>
</html>
如果答案符合你的要求,请采纳。有问题请追问。
更多追问追答
追问
在子类list中需要包含父类的信息
追答
function encodeJson (a) {
//用来存放新的数组
var b = [];
//用来存放store_id,方便查找
var c = [];
var i=0,j=0,k=-1;
for(;i<a.length;i++){
var id = a[i]['store_id'];
for(j=0;j<c.length;j++){
if(c[j]==id){
k = j;
break;
}
}
if(k!=-1){
b[k].list.push(a[i])
}else{
b.push(a[i]);
var len = b.length;
b[len-1].list = [];
b[len-1].list.push(a[i]);
c.push(id);
}
k=-1;
}
return b;
}
这也很简单,只要在else语句中将a[i]添加到子项中就可以了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询