Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/3624741/searching-for-objects-in-javascript-arrays/3624761#3624761">Searching for objects in JavaScript arrays</a></p> <pre><code>javascript: /* quick fix naive short solution to be posted soon */ /* array of objects with primitive property values only and no wrinkles */ </code></pre> <p>.</p> <pre><code>javascript: alert( JSON.stringify( [{x:1,y:2},,,{p:"q"},{u:{},vx:[],x:{y:{},x:5}}] ) . match(/"x":/g) ) </code></pre> <p>and</p> <pre><code>javascript: /* Does the need to fortify this code imply JSON is stronger? */ alert( /* See wrinkles below */ [{x:1,y:2},,,{p:"q"},{u:{},vx:[],x:{y:{},x:5}}] . toSource() . /* match(/({|,)\s*x:/g) . join() . replace(/({|,)\s*x:/g,"x:") finds `x:,x:,x:` */ replace(/(({|,)\s*)([^,{:]*):/g,'$1"$3":') . match(/"x":/g) ) </code></pre> <p>find <code>"x":,"x":,"x":</code>.</p> <p>Specific property found, done deal?</p> <p>Hint, hint (but must be appropriately attenuated and amputated for nested animals):</p> <pre><code>javascript: alert( JSON.stringify([{x:1,y:2},{p:"q"},{u:{},v:[],x:{y:{},x:5}}]) . match(/"[^"]*":/g) ) </code></pre> <p>finds <code>"x":,"y":,"p":,"u":,"v":,"x":,"y":,"x":</code> (all properties - Done Now?) </p> <p>More (a lot more) brain strain pain will find <code>x:</code>values and index of array positions (Hint count top level <code>,</code>'s).</p> <p>Amputate and attenuate hint (only removes nested array and object <code>,</code>'s, see wrinkles):</p> <pre><code>javascript:debug=false; animal=[ {x:1,y:2},,,{p:"q"}, [ {u:{},vx:[,,], x:{y:{xx:''},x:5} }, "hmmm comma x colon \" monster" ], ]; animal=animal.toSource().replace(/\[(.*)]/,"$1"); /* */ if(debug){ alert(animal); animal=animal.replace(/\[([^[\]]*)\]/g, function(a,b,c,d){alert([a,b,c,d].join("\n\n"));return a}); while(animal.search(/\{.*\}|\[.*\]/)&gt;-1){ animal=animal.replace(/\{([^{}]*)\}|\[(.*)\]/g, function(a,b,c,d){alert([a,"\n",b,"\n",c]);return b.replace(/,/g,";")}); alert(animal); } /* */ } /* the while loops on nesting depth not top array length */ while(animal.search(/\{.*\}|\[.*\]/)&gt;-1) animal=animal.replace(/\{([^{}]*)\}|\[(.*)\]/g, /* implicit g loop */ function(a,b,c,d){return (b+c).replace(/,/g," ")}); /* ditto */ alert(animal); /* as opposed to a non-lert animal? */ </code></pre> <p>Wrinkles:</p> <ul> <li><code>.toSource()</code> IS stronger (but ... see above) and handles more situations than <code>JSON</code><br> ref: <a href="https://stackoverflow.com/questions/171407/implementing-mozillas-tosource-method-in-internet-explorer/5628008#5628008">Implementing Mozilla&#39;s toSource() method in Internet Explorer</a><br /><br /></li> <li>what if there are monsters with strings containing: <ol> <li><code>,</code>'s . . . as in . . . <code>[",,or",,{p:"1,2,3,"}]</code> </li> <li><code>{x:...}</code> or <code>{"x":...}</code> . . . as in . . . <code>['{"x":...}'," and ","{x:...}",,]</code><br> (which will screw up the above coding using either <code>JSON</code> or <code>toSource</code>)</li> <li>nested monsters </li> <li>other monstrosities are mere chimeras ... not paid enough to do or prove</li> </ol></li> </ul>
    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.
    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