好友
阅读权限 25
听众
最后登录 1970-1-1
木木头上
发表于 2022-10-13 18:56
本帖最后由 木木头上 于 2022-10-15 13:29 编辑
原始数据
[JavaScript] 纯文本查看 复制代码
var arr = [
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001", "name": "手机", "city": "上海", "value": [1,2]},
{"id":"1002", "name": "电脑", "city": "上海", "value": 2},
{"id":"1002", "name": "电脑", "city": "北京", "value": [3,4,8]},
{"id":"1002", "name": "电脑", "city": "杭州", "value": 2}
]
想要得到的json数据
[JavaScript] 纯文本查看 复制代码
var arr = [
[
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001","name":"手机","data":[{"city":"上海","value":[1,2]}]},
{"id":"1002","name":"电脑","data":[{"city":"上海","value":2},{"city":"北京","value":[3,4,8]},{"city":"杭州","value":2}]}
]
通过js或者php怎么进行转化,有多个相同的值,解析成树感觉好难
新的转换方式:怎么达到下面的效果,形成json树
[JavaScript] 纯文本查看 复制代码
//原始数据
var arr = [
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001", "name": "手机", "city": "上海", "value": [1,2]},
{"id":"1002", "name": "电脑", "city": "上海", "value": 2},
{"id":"1002", "name": "电脑", "city": "北京", "value": [3,4,8]},
{"id":"1002", "name": "平板", "city": "杭州", "value": 2}
];
//需要转换后结果:
//始终保持唯一id,对name和city等进行分组合并。
[
{"id": "1000",
"tree": [
{"name": "监控","data": [
{"city": "成都","value": 9}
]
}
]
},
{"id": "1001",
"tree": [
{"name": "手机","data":[
{"city": "上海","value": [1,2]}
]
}
]
},
{"id": "1002",
"tree": [
{"name": "电脑",
"data": [
{"city": "上海","value": 2},
{"city": "北京","value": [3,4,8]},
]
},
{"name": "平板",
"data": [
{"city": "杭州","value":2}
]
}
]
}
]
免费评分
查看全部评分