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":'),
      );
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章