I recently wrote some functions in the global jQuery namespace that add array utilities. The list is small right now (only 3) but I hope to grow them as I have time, and can optimize as well. They are inspired by some useful PHP array functions that I have missed on the client-side.
In browser-based JavaScript arrays are underutilized, largely because of the lack of good cross-browser functions. jQuery has added some to the browser programmer’s arsenal, such as $.map, $.grep, $.merge, and $.inArray. This adds the following functions:
- $.randArray – Returns a randomly reordered array, given the original and optional number of elements to return (see php.net/array_rand)
- $.uniqueArray – Returns a de-duped array, given the original (see php.net/array_unique)
- $.keysArray – Returns an array consisting of the keys of the provided array or object (see php.net/array_keys)
That last one is a bit different, right?
That’s because in PHP, associative arrays and numerically indexed arrays are one and the same. In JavaScript, arrays are more like lists, and they are always numerically indexed. Associative arrays are represented as object literals. So consider yourselves warned: This sort of typecasting is unavoidable.
Consider a JavaScript counterpart to the PHP function array_flip() – if you pass an array, the values must become the keys. This cannot be represented as an array any longer in JavaScript – it must become an object. The same may not be true of an object with numeric values; it could technically be represented as an array. But while the input type may vary, I do not believe the output should. So $.flipArray (let’s stay consistent with naming) would return an object, no matter what.
I hope you can use, and perhaps extend further extend these functions. They are listed below.
/**
* This plugin adds additional array functions to the
* jQuery global object.
*/
jQuery.extend(
(function($){
return {
/**
* This function returns random members of
* the array provided, up to the number in
* the count provided.
*/
randArray: function(arr, count) {
var randomNumber = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
rand_arr = [],
rand, i;
count = (count && (count > arr.length ? arr.length : count)) || 1;
for(i = 0; i < count; i++) {
rand = randomNumber(0, arr.length - 1);
rand_arr[i] = arr[rand];
arr.splice(rand, 1);
}
return rand_arr;
},
/**
* This function removes duplicate members
* of the array provided.
*/
uniqueArray: function(arr) {
var unique_arr = [];
return $.grep(arr, function(elem, index) {
unique_arr.push(elem);
return $.inArray(arr[index], unique_arr) === index;
});
},
/**
* This function returns the keys of an array
* or an object provided.
*/
keysArray: function(arrOrObj) {
var type, i, len, retArr = [];
// The easy way
if (typeof Object.keys === 'function') {
return Object.keys(arrOrObj);
}
// The slightly-less-easy way
else {
type = Object.prototype.toString.call(arrOrObj);
if (type === '[object Array]') {
for (i = 0, len = arrOrObj.length; i < len; i++) {
retArr.push(String(i));
}
}
else if (type === '[object Object]') {
for (i in arrOrObj) {
if (arrOrObj.hasOwnProperty(i)) {
retArr.push(String(i));
}
}
}
return retArr;
}
}
};
}(jQuery))
);