Please go to Fame Web Development for freelance jobs or paid support for the projects listed on this website.

Nick Van der Vrekens blog & portfolio

Extended javascript Array Class - com.bydust.array

October 6th, 2008

Browsers, Javascript, Portfolio

The extended javascript Array class is a small class that allows you to access more - custom - javascript methods, for example array.contains(object), array.dump(), array.shuffle() and others. It can be used for faster programming and fixing crossbrowser issues. Nothing fancy really, I just scripted this because I thought the normal Array class could use a few extra’s.

Updated to version 1.1 on october 29th, 2008

So what’s in this “extended class”?

Well, the extended javascript Array class contains some pretty usefull functions that are not included in the standard javascript Array class. Here’s a full list of what is standard included and which functions and methods are added by this class.

Standard Array functions:

  • array.concat(another_array, …);
  • array.join(”separator”);
  • array.pop();
  • array.push(obj, …);
  • array.reverse();
  • array.shift();
  • array.slice(start[, end]);
  • array.sort();
  • array.splice(index, howmany[, elementstoadd]);
  • array.toSource();
  • array.toString();
  • array.unshift(elements);
  • array.valueOf();

Functions and methods added in com.bydust.array 1.1:

  • array.contains(obj);
    • Returns true if the array contains the given value, false if it doesn’t.
  • array.count();
    • Counts the number of keys in the array.
  • array.countValues();
    • Returns an array using the values of the array as keys and their frequency in input as values.
  • array.dump();
    • Returns a string containing a table with each key and its respective value in HTML-code.
  • array.fillKeys(keys, value);
    • Fills an array with the value of the value parameter, using the values of the keys array as keys.
  • array.get(key);
    • Returns the value attached to the specified key.
  • array.getKeys();
    • Returns an array containing all the keys.
  • array.indexOfKey(key);
    • Returns the index of the specified key in the array, returns -1 if the key doesn’t exist.
  • array.indexOf(obj);
    • Returns the index of the specified value in the array, returns -1 if the value doesn’t exist.
  • array.keyOnIndex(index);
    • Returns the key on the specified index.
  • array.makeUnique();
    • Removes duplicate values from an array.
  • array.product();
    • Calculate the product of values in an array.
  • array.shuffle([preserve_keys]);
    • Shuffle the array. Optional parameter “preserve_keys”, default false.
  • array.set(key, value);
    • Add a key with a value.
  • array.sum();
    • Calculate the sum of values in an array.
  • array.swap(key1,key2);
    • Swaps two elements in an array. Keys are not preserved.
  • array.rand();
    • Picks a random entry out of an array.
  • array.remove(key);
    • Removes the specified key and its value from the array.

There’s also a fix for the array.indexOf(object) method. If the given object is found in the array, it will return its index as a number. If not, -1 is returned. This method is already implemented in the normal Array class, but doesn’t work for IE browsers. Well, now it does :)

Well, I hope someone can use this extended class. I wasn’t going to put it online at first, but changed my mind when I saw I’m not the only one who could use this :)

You can download the class here.

Subscribe for updates

Leave us your name and email adress and we'll inform you when someone posts a new comment. unsubscribe?




There are 7 replies to this item:




XHTML:Please use "<pre>" to insert code: <pre lang="actionscript asp css html4strict javascript php sql xml[pick any of these languages]" line="45[starting line, optional]"> your code here </pre>

  • #7

    March 21st, 2010

    Ivo says:

    Maaan, this is amazing! Thank you so much! I lost my mind for two days trying to solve a problem and your class saved my ass. :) You rock! :-)

  • #6

    October 8th, 2009

    Nick says:

    Oops, my bad. :)
    I have no idea why it dissapeared, but it’s back now. Thanks for the reply ;-)

  • #5

    October 8th, 2009

    fwolf says:

    There IS NO LINK!

    cu, w0lf.

  • #4

    November 5th, 2008

    Nick says:

    Hi D :)

    The array.rand() function returns a random value in the array, not a key.

    cheers,

    Nick

  • #3

    November 5th, 2008

    D says:

    nice work.
    how does the rand() function work? does it pick a random value from the array or a random key ?

  • #2

    October 29th, 2008

    Nick says:

    Updated to version 1.1 on october 29th, 2008.
    Added new functions array.shuffle(), array.countValues(), array.fillKeys(), array.makeUnique(), array.rand(), array.sum() and array.product().

  • #1

    October 20th, 2008

    AJ says:

    thanks for this, the array.dump() and array.contains() are very usefull.