Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I programmed something that should deal with your problem. If I understood you right, this is exactly what you need.</p> <p>I solved this problem using recursion and the visitor pattern. Works like a charm. I didn't test it for all possible types but missing types can be inserted easily when needed. Arrays do crash currently - you will need to catch this if they appear too.</p> <p>Some explanation:</p> <p>1) I tested the types of the values.</p> <p>2) I initialized an array that store values I could find.</p> <p>3) I wrote a recursive method that tests object-properties for being objects</p> <p>4) If the property is a object it will be used recursively in the same method.</p> <p>5) If the property is no object, it's data will be added to the previously initialized array.</p> <p>6) After the recursive method's executing I debug the array and create a sample output. </p> <pre><code>// the object to use: var o = {a:1, b:{c:"2", d:"3"}} // some type testing: //alert(typeof(o.a)); // string //alert(typeof(o.b)); // object // implement a recursive method that reads all // the needed stuff into a better-to-handle array. function readAttributesRecursive(obj, arr) { for(prop in obj) { // get the value of the current property. var propertyValue = obj[prop]; // get the value's type var propertyValueType = typeof(propertyValue); // if it is no object, it is string, int or boolean. if(propertyValueType !== 'object') { arr.push({ property : prop, value : propertyValue, type : propertyValueType // just for debugging purposes }); } // otherwise it is a object or array. (I didn't test arrays!) // these types are iterated too. else { // red the object and pass the array which shall // be filled with values. readAttributesRecursive(propertyValue, arr); } } } // END readAttributesRecursive(obj, arr) // ok, lets get the values: var result = new Array(); readAttributesRecursive(o, result) console.debug(result); // the result looks like this: // [ // { property : "a", type : "number", value: "1" } // { property : "c", type : "string", value: "2" } // { property : "d", type : "string", value: "3" } // ] // And now do the &lt;div&gt;-stuff: var div = '&lt;div'; for(i = 0; i &lt; result.length; i++) { var data = result[i]; div += ' ' + data.property + '="' + data.value + '"'; } div += "&gt;Some text&lt;/div&gt;"; console.debug(div); </code></pre> <p>Note: Please do never create HTML-elements like this (using strings)! Use document.createElement() and work with created DOM-elements. Using strings can result in strange behaviour , bugs and less readable code ... (strings are not EXACTLY treated like DOM-elements after being inserted into DOM)</p>
    singulars
    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.
 

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