{"id":232,"date":"2014-06-01T16:52:02","date_gmt":"2014-06-01T16:52:02","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=232"},"modified":"2014-06-06T09:21:28","modified_gmt":"2014-06-06T09:21:28","slug":"extender-array","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=232","title":{"rendered":"extender &#8211; Array"},"content":{"rendered":"<blockquote><p>Goal: give some additional function to the Array type<\/p><\/blockquote>\n<p><code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\"><\/code><br \/>\n\/\/ 1. remove the specified element<br \/>\nTypeExtend(Array, 'remove', function(from, to) {<br \/>\n  try{<br \/>\n    var rest = this.slice((to || from) + 1 || this.length);<br \/>\n    this.length = from < 0 ? this.length + from : from;\n    return this.push.apply(this, rest);\n  }\n  catch(e){\n    return false;\n  }\n});\n\n\/\/ 2. remove all listed elements (passed as arguments)\nTypeExtend(Array, 'removeA', function() {\n  var what, a = arguments, L = a.length, ax;\n  while (L &#038;&#038; this.length) {\n    what = a[--L];\n    while ((ax = this.indexOf(what)) !== -1) {\n      this.splice(ax, 1);\n    }\n  }\n  return this;\n});\n\n\/\/ 3. shuffle the items\nTypeExtend(Array, 'shuffle', function() {\n  var i = this.length, j, temp;\n  while ( --i )\n  {\n    j = Math.floor( Math.random() * (i - 1) );\n    temp = this[i];\n    this[i] = this[j];\n    this[j] = temp;\n  }\n  return this;\n});\n\n\/\/ 4. return a new array with distincted items\nTypeExtend(Array, 'distinct', function() {\n  var map = {}, out = [];\n  for(var i=0, l=this.length; i<l; i++){\n    if(map[this[i]]){ continue; }\n    out.push(this[i].toString());\n    map[this[i]] = 1;\n  }\n  return out;\n});\n\n\/\/ 5. check is item is in the array ( where item[tag] == value )\nTypeExtend(Array, 'containsEx', function(tag, value) {\n  for(var i=0, l=this.length; i<l; i++){\n    if(this[i][tag] == value){return true;}\n  }  \n  return false;\n});\n\n\/\/ 6. get item by ID ( return item[tag] == value )\nTypeExtend(Array, 'getBy', function(tag, value) {\n  for(var i=0, l=this.length; i<l; i++){\n    if(this[i][tag] == value){return this[i];}\n  }  \n  return null;\n});\n\n\/\/ 7. return a new array with the sorted elements\nTypeExtend(Array, 'sortBy', function(prop, asc) {\n  var out = [];\n  out = this.sort(function(a, b) {\n      if (asc) return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);\n      else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);\n    });\n  return out;\n});\n\n\/\/ 8. drop all undefined elements from the array\nTypeExtend(Array, 'shrink', function() {\n  var ln = this.length;\n  for(var i=0, l=this.length; i<l; i++){\n    this[i] != undefined &#038;&#038; this.push(this[i]);\n  }\n  this.splice(0,ln);\n  return this;\n});\n[\/js]\n\n\n\n<blockquote>see also: <a href=\"?p=23\" title=\"javascript extender (TypeExtend)\">javascript extender (TypeExtend)<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Goal: give some additional function to the Array type \/\/ 1. remove the specified element TypeExtend(Array, &#8216;remove&#8217;, function(from, to) { try{ var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); } catch(e){ return false; } }); \/\/ 2. remove all [&hellip;]\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":6,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}