JSON.stringify 过滤字段

 

let obj = {name: 'lili', age: 21, children: [{name: 'lucy', age: 18}]}
let str = JSON.stringify(obj, ["name", "children"])

console.log(str) // '{"name":"lili","children":[{"name":"lucy"}]}'
// 很有用,并且很方便,自动递归
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

可以替换

const obj = JSON.parse(
        JSON.stringify(resData)
          .replace(/"subList":/g, '"children":')
          .replace(/"id":/g, '"value":')
          .replace(/"name":/g, '"label":'),
      );
 
 
 
posted @ 2022-05-21 23:23  James2019  阅读(254)  评论(0编辑  收藏  举报