WEBDOOD.COM

Tag: array prototype methods

Javascript Array.remove and Array.indexOf methods

by webdood on Jan.01, 2011, under Javascript, Software Development

I kept finding myself needing to remove items from an array, so developed a quick helper (prototype) method attached to the Array Object to help.
////////////////////////////////////////////////////////////////////////////////
//
// Array.remove( object|string item) - removes an item from an array
//    Example x = ["abc","xyz",1,4]  x.remove("xyz") returns ["abc",1,4]
//    Presumably this will eventually be added to Javascript, but until that day...
////////////////////////////////////////////////////////////////////////////////
if (Array.prototype.remove===undefined) {
  Array.prototype.remove = function( item ) {
    var itemLocation = this.indexOf(item);
    if (itemLocation > -1) {
      this.splice(itemLocation,1);
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
//
// Array.indexOf() - returns integer index where valueToSearchFor is in an Array
//   (believe it or not, not all browsers have this yet ... and it's 2010!
////////////////////////////////////////////////////////////////////////////////
if (Array.prototype.indexOf===undefined) {
  Array.prototype.indexOf = function( valueToSearchFor ) {
    var iEnd = this.length;
    var retVal = -1;
    for (var i=0;i<iEnd; i++) {
      if (this[i] == valueToSearchFor) {
        retVal = i;
        break;
      }
    }
    return retVal;
  };
}

Shannon Norrell

Leave a Comment :, , more...

Javascript Array Detection

by webdood on Oct.06, 2010, under Javascript, Software Development

Turns out the most reliable way to detect an Array is not to use the old standby:

return (typeof foo === ‘object’ && foo.constructor === Array)

but rather to do this:

function isArray(anArray) {
  return Object.prototype.toString.apply(anArray) === "[object Array]";
}

I have to give credit to my friend Doug Crockford, for this.

I recommend his book Javascript: The Good Parts

shannon norrell

Leave a Comment :, , more...

Javascript: Flatten an array containing arrays

by webdood on Sep.08, 2010, under Javascript, Software Development

This quick function takes a an array, whose elements may or may not be other arrays, and flattens it into a single array.

Extremely simple solution, but kind of fun because it’s a chance to use recursion:

<script type=text/javascript>
var a = [1,2,[5,6,7], 8, [9,10,[11,12],13], 14];

function flatten( oArray ) {
  var retVal = [];
  for (var i=0;i<oArray.length;i++) {
    if (!isArray( oArray[i]) ) {
      retVal.push( oArray[i] );
    } else {
      var tempFlatt = flatten(oArray[i]);
      for (var j=0;<tempFlatt.length;j++) {
        retVal.push( tempFlatt[j] );
      }
    }
  }
  return retVal;
}

function isArray( anElement ) {
  return (typeof anElement=="object" && anElement.constructor == Array);
}

alert(flatten(a));
</script>
2 Comments :, , more...

Fast JavaScript Max/Min on an Array Taking Arbitrary Number of Arguments

by webdood on Feb.25, 2009, under Javascript, Software Development

What's the fastest way to find the largest, or smallest, number in an array?

Array.max = function( array ){
  return Math.max.apply( Math, array );
};
Array.min = function( array ){
  return Math.min.apply( Math, array );
};

By using JavaScript's .apply() method on a built-in function you can pass in an array of unlimited arguments.

Shannon Norrell

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

  • A List Apart
  • Dive into HTML5
  • Javascript: The Good Parts
  • QuirksMode.org