请教一个分析json数据的问题

ahking 2021-03-21 09:52:57
如下的json,想根据id判断其对应的节点下面children里面有没有内容或有没有children


//模拟数据1
,data1 = [{
title: '江西'
,id: 1
,children: [{
title: '南昌'
,id: 1000
,children: [{
title: '青山湖区'
,id: 10001
},{
title: '高新区'
,id: 10002
}]
},{
title: '九江'
,id: 1001
},{
title: '赣州'
,id: 1002
}]
},{
title: '广西'
,id: 2
,children: [{
title: '南宁'
,id: 2000
},{
title: '桂林'
,id: 2001
}]
},{
title: '陕西'
,id: 3
,children: [{
title: '西安'
,id: 3000
},{
title: '延安'
,id: 3001
}]
}]
...全文
236 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahking 2021-03-24
  • 打赏
  • 举报
回复
学习了。。。。。。
一品梅 2021-03-22
  • 打赏
  • 举报
回复
一品梅 2021-03-22
  • 打赏
  • 举报
回复
js中"!!"这个是什么意思呢? 这是套路 , 如何把一个任意类型的值转换为布尔类型? 用!! 一个!是取非 再一个!又取非 相当于把这个数据转换为boolen类型了
const newItem = {
            id: item.id,
            title: item.title,
            children:!!item.children  //把真值转换成了布尔值
        };
泡泡鱼_ 2021-03-22
  • 打赏
  • 举报
回复
类型声明去掉,加个默认值,用function代替箭头函数就行了…… 保留children数据

function transform2Json(data, json) {
    json = json || {};
    data.forEach(function(item) {
        let newItem = {
            id: item.id,
            title: item.title
        };
        item.children && (newItem.children = item.children);
        json[item.id] = newItem;
        item.children && transform2Json(item.children, json);
    });
    return json;
};
const json = transform2Json(data);
//打印
console.log(json);
//判断id=1时是否存在children
console.log('id=1 =>', !!json[1].children);
//判断id=10001时是否存在children
console.log('id=10001 =>', !!json[10001].children);
不保留children数据,转换为布尔类型

function transform2Json(data, json) {
    json = json || {};
    data.forEach(function(item) {
        let newItem = {
            id: item.id,
            title: item.title,
            children:!!item.children
        };
        json[item.id] = newItem;
        item.children && transform2Json(item.children, json);
    });
    return json;
};
const json = transform2Json(data);
//打印
console.log(json);
//判断id=1时是否存在children
console.log('id=1 =>', json[1].children);
//判断id=10001时是否存在children
console.log('id=10001 =>', json[10001].children);
ahking 2021-03-22
  • 打赏
  • 举报
回复
这是ts语法,没用过,不知怎么转成js
泡泡鱼_ 2021-03-22
  • 打赏
  • 举报
回复
引用 4 楼 ahking 的回复:
有点复杂,知识盲区了
????就直接把你的data当做参数传进去调用就行了呀
ahking 2021-03-22
  • 打赏
  • 举报
回复
有点复杂,知识盲区了
泡泡鱼_ 2021-03-21
  • 打赏
  • 举报
回复

/**
 * 将源数组形态进行改变,拉平层级,并以id做为单项数据的属性名称以便于直接查找
 * @param data {any[]} 待遍历数组:初始时源数组,内部查找时为对应的children节点数据
 * @param json {Record<number,any>} 待返回的新数据对象,默认值为{};它由源数组变化而来,数据结构如下:
 * @example
 * {
 * 1:{fid:0,id:1,title:"xx",children:[]||false},
 * 1000:{fid:1,id:1000,title:"xxxx",children:[]||false}
 * }
 */
function transform2Json(data: any[], json: Record<number, any> = {}) {
    data.forEach(item => {
        let newItem: {id: number, title: string, children?: any[]} = {
            id: item.id,
            title: item.title
        };
        item.children && (newItem.children = item.children);
        json[item.id] = newItem;
        item.children && transform2Json(item.children, json);
    });
    return json;
}
const json = transform2Json(data);
//打印
console.log(json);
//判断id=1时是否存在children
console.log('id=1 =>',!!json[1].children);
//判断id=10001时是否存在children
console.log('id=10001 =>',!!json[10001].children);
其中为了后续可以直接获取children,所以children部分的数据是有冗余的。如果只是需要做个简单判断,不需要保留到后续使用,可将

let newItem: {id: number, title: string, children?: any[]} = {
            id: item.id,
            title: item.title
        };
        item.children && (newItem.children = item.children);
改为:

const newItem = {
            id: item.id,
            title: item.title,
            children:!!item.children
        };

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧