Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no good way to detect circularity in objects but it is possible though by walking the object tree and checking references. I baked up a node-walking function that tries to detect if a node has been already used as its parent</p> <pre><code>function isCircularObject(node, parents){ parents = parents || []; if(!node || typeof node != "object"){ return false; } var keys = Object.keys(node), i, value; parents.push(node); // add self to current path for(i = keys.length-1; i&gt;=0; i--){ value = node[keys[i]]; if(value &amp;&amp; typeof value == "object"){ if(parents.indexOf(value)&gt;=0){ // circularity detected! return true; } // check child nodes if(arguments.callee(value, parents)){ return true; } } } parents.pop(node); return false; } </code></pre> <p>And the usage would be <code>isCircularObject(obj_value)</code> where the function returns <code>true</code> if circularity exists and <code>false</code> if not.</p> <pre><code>// setup test object var testObj = { property_a:1, property_b: { porperty_c: 2 }, property_d: { property_e: { property_f: 3 } } } console.log(isCircularObject(testObj)); // false // add reference to another node in the same object testObj.property_d.property_e.property_g = testObj.property_b; console.log(isCircularObject(testObj)); // false // add circular node testObj.property_b.property_c = testObj.property_b; console.log(isCircularObject(testObj)); // true </code></pre> <p>The key point being that an object value is equal with an other value <em>only</em> if it is the same object reference and not when it's another object (even if completely similar).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload