JSON内多个数组相同值合并问题求助
本帖最后由 木木头上 于 2022-10-15 13:29 编辑原始数据
var arr = [
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001", "name": "手机", "city": "上海", "value": },
{"id":"1002", "name": "电脑", "city": "上海", "value": 2},
{"id":"1002", "name": "电脑", "city": "北京", "value": },
{"id":"1002", "name": "电脑", "city": "杭州", "value": 2}
]
想要得到的json数据
var arr = [
[
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001","name":"手机","data":[{"city":"上海","value":}]},
{"id":"1002","name":"电脑","data":[{"city":"上海","value":2},{"city":"北京","value":},{"city":"杭州","value":2}]}
]
通过js或者php怎么进行转化,有多个相同的值,解析成树感觉好难{:1_937:}
新的转换方式:怎么达到下面的效果,形成json树
//原始数据
var arr = [
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001", "name": "手机", "city": "上海", "value": },
{"id":"1002", "name": "电脑", "city": "上海", "value": 2},
{"id":"1002", "name": "电脑", "city": "北京", "value": },
{"id":"1002", "name": "平板", "city": "杭州", "value": 2}
];
//需要转换后结果:
//始终保持唯一id,对name和city等进行分组合并。
[
{"id": "1000",
"tree": [
{"name": "监控","data": [
{"city": "成都","value": 9}
]
}
]
},
{"id": "1001",
"tree": [
{"name": "手机","data":[
{"city": "上海","value": }
]
}
]
},
{"id": "1002",
"tree": [
{"name": "电脑",
"data": [
{"city": "上海","value": 2},
{"city": "北京","value": },
]
},
{"name": "平板",
"data": [
{"city": "杭州","value":2}
]
}
]
}
] 很高大上呀!赶快观看大师们如何解决。也有类似的合并问题,来学习一下解决方法! 这个还真有点难度呢,要写算法了 zlf2020999 发表于 2022-10-13 19:19
很高大上呀!赶快观看大师们如何解决。也有类似的合并问题,来学习一下解决方法!
网络抓包的json数据大多数都要进一步处理,想要合并成容易解析的树,自己循环蒙圈了 个人写法:
var arr = [
{"id":"1000", "name": "监控", "city": "成都", "value": 9},
{"id":"1001", "name": "手机", "city": "上海", "value": },
{"id":"1002", "name": "电脑", "city": "上海", "value": 2},
{"id":"1002", "name": "电脑", "city": "北京", "value": },
{"id":"1002", "name": "电脑", "city": "杭州", "value": 2}
];
var rs = {};
arr.forEach(e => {
let item = rs || {};
let data = item.data || [];
data.push({"city": e["city"], "value": e["value"]});
item = { "id": e["id"], "name": e["name"], "data": data };
rs = item;
});
Object.values(rs); 小白的写法
var xx = []
var yy = []
for(var i = 0 ;i<arr.length;i++){
if(yy.indexOf(arr.name)>=0){
var x = yy.indexOf(arr.name);
xx.data.push({"city":arr.city,"value":arr.value})
}else{
yy.push(arr.name)
xx.push({"id":arr.id,"name":arr.name,"data":[{"city":arr.city,"value":arr.value}]})
}
}
console.log(xx) 本帖最后由 kesai 于 2022-10-13 19:42 编辑
const { groupBy } = require("lodash")
var arr = [
{ "id": "1000", "name": "监控", "city": "成都", "value": 9 },
{ "id": "1001", "name": "手机", "city": "上海", "value": },
{ "id": "1002", "name": "电脑", "city": "上海", "value": 2 },
{ "id": "1002", "name": "电脑", "city": "北京", "value": },
{ "id": "1002", "name": "电脑", "city": "杭州", "value": 2 }
]
let group = groupBy(arr, "id");
let res = Object.entries(group).map(() => {
let name = arr.name;
let data = arr.map(v => new Object({
city: v.city, value: v.value
}))
return { id, name, data }
})
console.log(JSON.stringify(res)) kesai 发表于 2022-10-13 19:34
const { groupBy } = require("lodash")
var arr = [
这个是用了VUE的吧 本帖最后由 steven026 于 2022-10-13 19:59 编辑
不太优雅 凑合用吧
var arr = [
{ "id": "1000", "name": "监控", "city": "成都", "value": 9 },
{ "id": "1001", "name": "手机", "city": "上海", "value": },
{ "id": "1002", "name": "电脑", "city": "上海", "value": 2 },
{ "id": "1002", "name": "电脑", "city": "北京", "value": },
{ "id": "1002", "name": "电脑", "city": "杭州", "value": 2 }
]
let temp1 = arr.map(i => {
let { id, name, ...data } = i
return { id, name, data }
})
let temp2 = {}
temp1.forEach(i => {
if (temp2 == void 0) {
i.data =
temp2 = i
} else {
temp2['data'].push(i.data)
}
})
let final = Object.values(temp2) 木木头上 发表于 2022-10-13 19:51
这个是用了VUE的吧
没有,用了lodash啊,nodejs下就能运行
页:
[1]
2