JSON.stringify replacer - how to get full path

后端 未结 5 2073
太阳男子
太阳男子 2021-01-07 07:35

Replacer in below code write on console current processed field name

5条回答
  •  误落风尘
    2021-01-07 08:20

    There's just not enough information available in the replacer. These two objects have different shapes but produce the same sequence of calls:

    let c1 = { c1: 3, c2: 2 };
    let c2 = { c1: { c2: 3 } };
    
    const replacer = function (field, value) {
      console.log(field); // full path... ???
      return value;
    };
    
    JSON.stringify(c1, replacer);
    // logs c1, c2
    JSON.stringify(c2, replacer);
    // logs c1, c2
    

    You'll have to write something yourself.

提交回复
热议问题