吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1149|回复: 17
收起左侧

[求助] JSON内多个数组相同值合并问题求助

[复制链接]
回帖奖励 10 CB吾爱币 回复本帖可获得 1 CB吾爱币奖励! 每人限 1 次(中奖概率 80%)
木木头上 发表于 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}
]

未命名1665657813.png
想要得到的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}]}
]

未命名1665657719.png

通过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}
        ]
      }
    ]
  }
]

免费评分

参与人数 1吾爱币 +1 收起 理由
obitosec + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zlf2020999 发表于 2022-10-13 19:19
很高大上呀!赶快观看大师们如何解决。也有类似的合并问题,来学习一下解决方法!
kesai 发表于 2022-10-13 19:21

回帖奖励 +1 CB吾爱币

这个还真有点难度呢,要写算法了
 楼主| 木木头上 发表于 2022-10-13 19:22
zlf2020999 发表于 2022-10-13 19:19
很高大上呀!赶快观看大师们如何解决。也有类似的合并问题,来学习一下解决方法!

网络抓包的json数据大多数都要进一步处理,想要合并成容易解析的树,自己循环蒙圈了
大Z. 发表于 2022-10-13 19:28
个人写法:
[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}
];
var rs = {};
arr.forEach(e => {
    let item = rs[e.id] || {};
    let data = item.data || [];

    data.push({"city": e["city"], "value": e["value"]});

    item = { "id": e["id"], "name": e["name"], "data": data };
    rs[e.id] = item;
});
Object.values(rs);

免费评分

参与人数 2吾爱币 +4 热心值 +2 收起 理由
gorkys + 2 + 1 我很赞同!
木木头上 + 2 + 1 我很赞同!

查看全部评分

Piz.liu 发表于 2022-10-13 19:34

回帖奖励 +1 CB吾爱币

小白的写法
[JavaScript] 纯文本查看 复制代码
var xx = []
var yy = []
for(var i = 0 ;i<arr.length;i++){
    if(yy.indexOf(arr[i].name)>=0){
        var x = yy.indexOf(arr[i].name);
        xx[x].data.push({"city":arr[i].city,"value":arr[i].value})
    }else{
        yy.push(arr[i].name)
        xx.push({"id":arr[i].id,"name":arr[i].name,"data":[{"city":arr[i].city,"value":arr[i].value}]})
    }
}
console.log(xx)

免费评分

参与人数 1热心值 +1 收起 理由
木木头上 + 1 谢谢@Thanks!

查看全部评分

kesai 发表于 2022-10-13 19:34
本帖最后由 kesai 于 2022-10-13 19:42 编辑

[JavaScript] 纯文本查看 复制代码
const { groupBy } = require("lodash")

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 }
]
let group = groupBy(arr, "id");
let res = Object.entries(group).map(([id, arr]) => {
  let name = arr[0].name;
  let data = arr.map(v => new Object({
    city: v.city, value: v.value
  }))
  return { id, name, data }
})

console.log(JSON.stringify(res))

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
木木头上 + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| 木木头上 发表于 2022-10-13 19:51
kesai 发表于 2022-10-13 19:34
[mw_shl_code=javascript,true]const { groupBy } = require("lodash")

var arr = [

这个是用了VUE的吧
steven026 发表于 2022-10-13 19:52

回帖奖励 +1 CB吾爱币

本帖最后由 steven026 于 2022-10-13 19:59 编辑

不太优雅 凑合用吧
[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 }
]

let temp1 = arr.map(i => {
    let { id, name, ...data } = i
    return { id, name, data }
})

let temp2 = {}
temp1.forEach(i => {
    if (temp2[i.id] == void 0) {
        i.data = [i.data]
        temp2[i.id] = i
    } else {
        temp2[i.id]['data'].push(i.data)
    }
})

let final = Object.values(temp2)

免费评分

参与人数 1热心值 +1 收起 理由
木木头上 + 1 谢谢@Thanks!

查看全部评分

kesai 发表于 2022-10-13 19:53

没有,用了lodash啊,nodejs下就能运行
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表