recursive JSON.stringify implementation

后端 未结 6 1796
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 21:25

I am trying to learn recursion in Javascript, so I figured I\'d rewrite the native JSON.stringify function using recursion as a challenge to myself. I almost got my

6条回答
  •  时光说笑
    2021-02-04 22:13

    This has been answered several times but here is yet another solution:

    Using es6:

    let oldStringify = JSON.stringify;
    JSON.stringify = (obj, replacer, space) => oldStringify(obj, replacer || ((key, value) => {if(key && value === obj) return "[recursive]"; return value;}), space)
    

提交回复
热议问题