Detecting and fixing circular references in JavaScript

后端 未结 15 829
庸人自扰
庸人自扰 2020-11-28 23:29

Given I have a circular reference in a large JavaScript object

And I try JSON.stringify(problematicObject)

And the browser throws

15条回答
  •  萌比男神i
    2020-11-29 00:21

    I converted the answer of Freddie Nfbnm to TypeScript:

    export class JsonUtil {
    
        static isCyclic(json) {
            const keys = [];
            const stack = [];
            const stackSet = new Set();
            let detected = false;
    
            function detect(obj, key) {
                if (typeof obj !== 'object') {
                    return;
                }
    
                if (stackSet.has(obj)) { // it's cyclic! Print the object and its locations.
                    const oldIndex = stack.indexOf(obj);
                    const l1 = keys.join('.') + '.' + key;
                    const l2 = keys.slice(0, oldIndex + 1).join('.');
                    console.log('CIRCULAR: ' + l1 + ' = ' + l2 + ' = ' + obj);
                    console.log(obj);
                    detected = true;
                    return;
                }
    
                keys.push(key);
                stack.push(obj);
                stackSet.add(obj);
                for (const k in obj) { // dive on the object's children
                    if (obj.hasOwnProperty(k)) {
                        detect(obj[k], k);
                    }
                }
    
                keys.pop();
                stack.pop();
                stackSet.delete(obj);
                return;
            }
    
            detect(json, 'obj');
            return detected;
        }
    
    }
    

提交回复
热议问题