by

ArRAy heLPEr-OuTER....  yeah....

find the index of an object by the value of one of its properties...  pretty effn kewel!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/217249/ArRAy-heLPEr-OuTER------yeah----?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Array helper OutEr var getIDXByProp = function (arr, prop, val) { var a = arr, p = prop, v = val; if(a && p && v) { return getIndex(a,p,v); } function getIndex(a, p, v) { var i = 0; for (;i < a.length; i++) { if (a[i] == v) { return i; } else if (pathGet(a[i], p) == v) { return i; } } return -1; }; function pthGet (o, k, lft) { if ( lft === undefined ) { lft = k.split('.'); } var cur = lft.shift(); if ( typeof(o) == 'object' && o[cur] !== undefined ) { if ( lft.length === 0 ) { return o[cur]; } else if ( typeof(o[cur]) == 'object' ) { return arguments.callee(o[cur], undefined, lft); } } return null; } };
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus