JavaScript array
!No description available for array. @php.js developers: Please update the function summary text file.
1 2 3 4 56 7 8 9 10 | function array () { // !No description available for array. @php.js developers: Please update the function summary text file. // // version: 1008.1718 // discuss at: http://phpjs.org/functions/array // + original by: d3x // * example 1: array('Kevin', 'van', 'Zonneveld'); // * returns 1: ['Kevin', 'van', 'Zonneveld'] return Array.prototype.slice.call(arguments); } |
Examples
Running
1 | array('Kevin', 'van', 'Zonneveld'); |
Should return
1 | ['Kevin', 'van', 'Zonneveld'] |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have array goodness in JavaScript.
Perhaps the each() implementation I have below is a hasty one. It won't play nice with reset(), next() and previous(). It can be improved by actually storing the cursor position instead of consuming the key array with the shift(). I'll try to improve it and provide reset/next/prev if I find the time.
I apologize for the earlier, unformatted post :)
[CODE="Javascript"]
function each(arr) {
// Return the current key and value pair from an array and advance the array cursor
// + original by: Ates Goral (http://magnetiq.com)
// * example 1: each([42,43]);
// * returns 1: {0: 0, 1: 42, key: 0, value: 42}
// * example 2: each({a:"apple",b:"balloon"});
// * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}
if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {
return false;
}
if (!arr._keys) {
arr._keys = [];
for (var k in arr) {
if (k != "_keys") {
arr._keys.push(k);
}
}
}
var k = arr._keys.shift();
var v = arr[k];
return {
0: k,
1: v,
key: k,
value: v
};
}
[/CODE]
function each(arr) {
// Return the current key and value pair from an array and advance the array cursor
// + original by: Ates Goral (http://magnetiq.com)
// * example 1: each([42,43]);
// * returns 1: {0: 0, 1: 42, key: 0, value: 42}
// * example 2: each({a:"apple",b:"balloon"});
// * returns 2: {0:"a",1:"apple",key:"a",value:"apple"}
if (!(arr instanceof Object) || (arr._keys && !arr._keys.length)) {
return false;
}
if (!arr._keys) {
arr._keys = [];
for (var k in arr) {
if (k != "_keys") {
arr._keys.push(k);
}
}
}
var k = arr._keys.shift();
var v = arr[k];
return {
0: k,
1: v,
key: k,
value: v
};
}
@ Sean & Onno Marsman: I totally agree with Onno. If we can't get it right, and people have to learn a different notation anyway, let's just stick with JavaScript.
@Sean: We already can create associative arrays with JS:
[CODE="Javascript"]
{
'first': 'kevin',
'last': 'Zonnevelt' //I don't know why this is with a T ;)
}
[/CODE]
Technically this is not an array but an object, but your proposal wouldn't return anything different. So we already have a syntax that is "not quite the same as php" and it does exactly the same.
Also think of using variables:
[CODE="Javascript"]
{ a: b, c: d}
[/CODE]
Your alternative would result in something like this, because proper escaping is needed:
[CODE="Javascript"]
array(" '"+addslashes(a)+"' => '"+addslashes(b)+"' ", " '"+addslashes(c)+"' => '"+addslashes(d)+"' " );
[/CODE]
I hope I didn't make any typo's, but I think my point is clear.
@Kevin: Could you not do:
[CODE="Javascript"]
array(" 'first' => 'kevin' ", " 'last' => 'Zonnevelt' " );
[/CODE]
It's not quite the same as php, but at least this way you could create hash arrays
@ thinsoldier: Yes that would be very nice, but I don't see how we could implement that unfortunately :( Here we run against the hard wall of language differences.


Kevin van Zonneveld
10 Dec '08