当前位置: 首页 > 知识库问答 >
问题:

javascript - js递归如何生成该数据的预览json格式?

单于海荣
2023-11-23

需求就是通过json生成json,简化后的格式如下,如何通过递归的方式生成这些数据表示的最终格式?求教

//这个是各个类型生成的默认值const defaultVal = {    string:"默认值",    int:1,    float:0.1,    double:10.34,    boolean:false,    array:null}
//这个是json待转化的[    {        "type": "object",        "propertyName": "user",        "children": [            {                "type": "string",                "propertyName": "name",                "children": null            },            {                "type": "int",                "propertyName": "age",                "children": null,                            },            {                "type": "array",                "propertyName": "data",                "children": [                    {                        "type": "object",                        "name": "name",                        "children": null                    }                ]            }        ]    }]
//上述转化结果为:{    name:'默认值',    age:1,    data:[{}]}

共有3个答案

燕砚文
2023-11-23
const parseJson = (json: any) => {    if (Array.isArray(json)) {        const result = [] as any;        for (const item of json) {            if (item.propertyType === 'object') {                const obj = {} as any;                if (item.children) {                    for (const child of item.children) {                        if (child.propertyName) {                            obj[child.propertyName] = parseJson(child);                        }                    }                }                result.push(obj);            } else if (item.propertyType === 'array') {                const arr = parseJson(item.children);                if (arr && arr.length > 0) {                    result.push(arr);                } else {                    result.push(defaultVal[item.propertyType]);                }            } else {                result.push(defaultVal[item.propertyType]);            }        }        return result && result.length ? result : [];    } else if (json.propertyType === 'object') {        const obj = {} as any;        for (const child of json.children) {            if (child.propertyName) {                obj[child.propertyName] = parseJson(child);            }        }        return obj;    } else if (json.propertyType === 'array') {        return parseJson(json.children);    } else {        return defaultVal[json.propertyType];    }}

一种实现方式,试了下还可以,不知道有没有更好的方法

宋臻
2023-11-23

根据判断数据类型,下面简单的判断力一下,针对float等再详细判断

let data = {  name: "张三",  age: 15,  i: 19.8,  score: [{ english: 89.5, chinese: 97.0 }],};let list = this.getData(data, []);console.log(list)
getData(data, list) {  let obj = {};  for (let key in data) {    obj = {      type: typeof data[key],      propertyName: key,      children: null,    };    if (typeof data[key] !== "object") {      // 不是对象和数组      list.push(obj);    } else {      obj.children = [];      if (data[key] instanceof Array) {        data[key].forEach((item) => {          obj.children = this.getData(item,obj.children);        });      }      list.push(obj);    }  }  return list},``
马博学
2023-11-23
function toData(rule, defVal, init) {    const copy = val => JSON.parse(JSON.stringify(val));    const initial = init ?? copy(defVal[rule.type]);    return rule.children ? rule.children.reduce((res, v) => {      if(Array.isArray(res)) res.push(toData(v, defVal));      else if({}.toString.call(res) == '[object Object]') toData(v, defVal, res[v.propertyName] = copy(defVal[v.type]))      return res;  }, initial) : initial;}toData(  {    "type": "object",    "propertyName": "user",    "children": [      {        "type": "string",        "propertyName": "name",        "children": null      },      {        "type": "int",        "propertyName": "age",        "children": null,      },      {        "type": "array",        "propertyName": "data",        "children": [          {            "type": "object",            "name": "name",            "children": null          }        ]      }    ]  },  {    string:"默认值",    int:1,    float:0.1,    double:10.34,    boolean:false,    array:[],    object: {},  })
 类似资料:
  • 我正在尝试创建一个递归函数,该函数将生成项的嵌套结构。此文件中的每个项都有一个指向其子项的指针和一个停止值,如您可以在下面看到的: 这个递归函数应该获得一个开始索引,它将根据该索引构建树,并返回一个嵌套的字典,如下所示:

  • 我想通过js完成对表格进行插入数据。该如何处理。例如有个json数据,格式如下: 最终表格是三行4列,第4列数据是根据每行第3列的数据调用一个方法,然后输出到该行中的第四列中。

  • 上边格式如何转换为以下格式

  • js怎么根据一个视频的在线url生成一个视频预览图? 相当于要获取该视频的第一秒或者第一帧的图片。

  • 年代久远的项目,前台需要更新,后台因为没有人维护,所以数据结构无法更改 后台传递给前台的一个如下对象 要求把h0-h23的数据处理成echarts的x y轴的数据 例如 xData=["00:00","01:00"......] 一一对应

  • 问题内容: 我有一个扁平的json文件结构,例如: 我想要的是一个嵌套的文件结构,如: 对于应该深入多少层没有限制。我当前拥有的最大值是30。一个节点可以拥有的子级数量没有限制。例如。根节点将其余所有节点作为其子节点。 到现在为止我一直在尝试什么? 阅读有关d3.nest()的信息,以及它如何能够嵌套但并不完美。 https://groups.google.com/forum/?fromgroup