<script> var a1=[ ], a2=[ ], a3=[ ], a4=[ ], i, s1="äöüABC"; a1 = s1.split(''); for(i = 0; i < a1.length; i += 1) { a2.push( a1[i].charCodeAt(0).toString(32) ); } for(i = 0; i < a2.length; i += 1) { a3.push( parseInt(a2[i],32) ); } for(i = 0; i < a3.length; i += 1) { a4.push( String.fromCharCode(a3[i]) ); } </script>
var arr = ['A', 'B', 'C', 'D'];
debug(r, arr, i )
<script> function debug(r, arr, i) { var s = "\ni = III mit [ARR], length=LLL, arr[i]=VAL"; s = s.replace("III", i); s = s.replace("ARR", arr.join('|')); s = s.replace("LLL", arr.length) .replace("VAL", arr[i]); r.push( s ); alert( r[i] ); } var arr = ['A', 'B', 'C', 'D'], r = [ ]; debug(r, arr, 0 ); delete arr[1]; debug(r, arr, 1 ); arr.splice(1, 1); debug(r, arr, 2 ); </script>
debug(r, arr, 0 )
debug(r, arr, 1 )
debug(r, arr, 2 )
<script> var arr = [, ("42" == 42), ("42" === 42), ("42" != 42), ("42" !== 42) ]; arr[0] = typeof arr[0]; arr[6] = typeof arr[6]; arr[7] = !arr[5]; arr[5] = !arr[7]; alert(arr.join('\n')); </script>
<div id="my_test_div">/*1*/</div> <script> var ss = '', /*2*/ obj = { a:'A', 'b': 'B', 'c-char': 'C', '1': 123 }, arr = [ ]; arr.push(obj.a); /*3*/ arr.push(obj.b); arr.push(obj['a']); arr.push(obj['c-char']); ss = arr.join(' | '); window.onload = function() { /*4*/ document.getElementById('my_test_div').innerHTML = ss; }; </script>
arr.indexOf(SuchWert)
arr.lastIndexOf(SuchWert)
<script> var arr = [67, 'C', 0x42, 'B', 65, 'A']; var r = [arr.indexOf('A'), arr.lastIndexOf('A'), arr.indexOf('C', 2), arr.lastIndexOf('C', 2), arr.indexOf(65, 4), arr.lastIndexOf(65, 4) ]; </script>
arr.indexOf('A')
arr.lastIndexOf('A')
arr.indexOf('C', 2)
arr.lastIndexOf('C', 2)
arr.indexOf(65, 4)
arr.lastIndexOf(65, 4)
<script> function test(o) { return (o && o.length === +o.length); } var a = [ ]; a.push( test( [ 0, 1, 2, 3, 4 ] )); a.push( test( "abcdefghijklmno" )); a.push( test( { a:'A', b:'B' } )); a.push( test( 123456789 )); a.push( test( document )); a.push( 'a.length = ' + a.length ); alert('erg1 = [' + a.join(', ') + '];' ); a[0] = test( [ 0, 1, 2, 3, 4 ] ); a[1] = test( "abcdefghijklmno" ); a[2] = test( { a:'A', b:'B' } ); a[3] = test( 123456789 ); a[4] = test( document ); a[5] = 'a.length = ' + a.length; alert('erg2 = [' + a.join(', ') + '];' ); </script>
<script> function arr_to_obj(arr, val) { var i, a, obj = {}; for(i = 0; a = arr[i]; i += 1){ obj[a] = val + '=' +a; } return obj; } var obj = arr_to_obj(['0', 'a', "2", 1, 0], "key"); </script>
arr.every(callback_function [, thisArg])
<script> var val_gerade = function (val) { return !(val % 2); }, str_nicht_leer = function (str) { return (str && str !== ''); }, a = [ 0x42, 66, 68], b = [ 66, 67, 68], c = [ 'A', 'B', ], d = [ ], r = [ (a[0] === a[1]), a.every(val_gerade), b.every(val_gerade), c.every(str_nicht_leer), d.every(str_nicht_leer) ]; </script>
(a[0] === a[1])
a.every(val_gerade)
b.every(val_gerade)
c.every(str_nicht_leer)
d.every(str_nicht_leer)
<script> function test(obj) { return (obj && obj.length === +obj.length); } var a = [ ]; a.push( test( [ 0, 1, 2, 3, 4 ] )); a.push( test( "abcdefghijklmno" )); a.push( test( { a:'A', b:'B' } )); a.push( test( 123456789 )); a.push( test( document )); alert('erg1 = [' + a.join(', ') + '];' ); a[0] = test( [ 0, 1, 2, 3, 4 ] ); a[1] = test( "abcdefghijklmno" ); a[2] = test( { a:'A', b:'B' } ); a[3] = test( 123456789 ); a[4] = test( document ); alert('erg2 = [' + a.join(', ') + '];' ); </script>
arr.some(callback_function [, thisArg])
<script> var val_gerade = function (val) { return !(val % 2); }, str_nicht_leer = function (str) { return (str && str !== ''); }, a = [ 0x42, 66, 68 ], b = [ 66, 67, 68 ], c = [ 'A', 'B', ], d = [ ], r = [ a.some(val_gerade), b.some(val_gerade), c.some(str_nicht_leer), d.some(str_nicht_leer) ]; </script>
a.some(val_gerade)
b.some(val_gerade)
c.some(str_nicht_leer)
d.some(str_nicht_leer)
arr.filter(callback_function [, thisArg])
<script> function is_str(str) { return (typeof str === 'string'); }; function is_num(val) { return (typeof val === 'number'); }; var a = [0x41, 'A', 66, 'B', 67, "\u0043"], num = a.filter(is_num), str = a.filter(is_str); alert(num + '\n' + str) </script>
a.filter(is_num)
arr.map(callback_function [, thisArg])
<script> var a = [0x42, 67, 68], b = ['A', 1, 2], c = [66, 68, 70, 3], aa = a.map(function (el, i) { return 100 * el + i; }), bb = b.map(function (el) { return (~el >>> 0).toString(16); }), cc = c.map(function (el, i) { return c[i] - (a[i] || 0); }); </script>
a.map()
b.map()
c.map()
arr.forEach(callback_function [, thisArg])
<script> var a = [0x42, 67, 68], b = ['A', 1, 2], c = [1, 2, 3, 14]; a.forEach(function (el, i) { a[i] = 10 * el; }); b.forEach(function (el, k) { b[k] = (~el >>> 0).toString(16); }); while (c && c.length > 1) { c.forEach(function (el, i, c) { if (el && c[i + 1]) { c[i] = (el + c[i + 1]) / 2; } else { c.pop(); } }); } alert([a, b, c]); </script>
a.forEach()
b.forEach()
c.forEach()
c = [ 1.0, 2.0, 3.0, 14.0 ] c = [ 1.5, 2.5, 8.5 ] c = [ 2.0, 5.5 ] c = [ 3.75 ]
init_matrix(n)
<script> function init_matrix(n) { var i, j, a = [ ]; for(i = 0; i < n; i += 1) { a[i] = [ ]; for(j = 0; j < n; j += 1) { if (i + j +1 >= n) { a[i][j] = 10*i+j; } else { a[i][j] = 0; } } } return a; } </script>
var v0 = init_matrix(3)[0]
var v2 = init_matrix(3)[2]
var arr = init_matrix(3)
arr[0][0]= 0; arr[0][1]= 0; arr[0][2]= 2; arr[1][0]= 0; arr[1][1]=11; arr[1][2]=12; arr[2][0]=20; arr[2][1]=21; arr[2][2]=22;
<script> var arr = [ true, undefined, 'ABC', 4711 ]; var i, h, r = [ ]; for( i = 0; i < arr.length; i += 1 ) { h = arr[i] + 10; r.push( h + ' mit typeof=' + typeof h ); } </script>
arr
<script> var arr = [ [ '-', "auf " ], "Array", 'Elemente', 'Zugriff ' ]; alert( arr[3] + arr[0][1] + arr[1] + arr[0][0] + arr[2] ); </script>
this
<script> var arr = [ function () { return this.join('\n'); }, function (that) { return function () { return that; }; } (this), function (i) { return this[i]; }, function () { return this.length; }, 4711 ]; alert( arr[0](),arr[1](),arr[1],arr[2](4),arr[3]()oO0.join('\n') ); </script>
arr[0]()
function () { return this.join('\n'); } function () { return that; } function (i) {return this[i]; } function () {return this.length; } 4711
arr[1]()
[object Window]
arr[1]
function () { return that; }
arr[2](4)
4711
arr[3]()
5
obj_from_key_val_arr(key, val)
<script> function obj_from_key_val_arr(key, val) { var i, o = {}; for (i = 0; i < key.length; i += 1) { if (typeof val !== 'undefined') { o[key[i]] = val[i]; } } return o; } var obj = obj_from_key_val_arr( ['a', 'b', '0', ''], ['A', 0, '0', 4711] ); </script>
alert( obj['a'] )
alert( (obj['b'] === obj.b) )
true
alert( obj['0'] )
0
alert( obj[''] )
<script> if (!Function.prototype.method) { /* 1 */ Function.prototype.method = function (func_name, func) { if (!this.prototype[func_name]) { this.prototype[func_name] = func; } }; } Array.method('loop', function (fn) { /* 2 */ var i, arr = this, r = []; for ( i = 0; i < arr.length; i += 1 ) { r.push( fn.call( arr, arr[i], i )); } return r; }); var sum = 0, /* 3 berechne r1, r2, r3, r4, r5 */ r1 = [10,11,12].loop( function (el, i){return el + 10;}), r2 = [10,11,12].loop( function (el, i){return 20 + this[i];}), r3 = [10,11,12,13 ].loop( function (el, i) { var a = this; switch(i){ // a[i] entspricht el case 0: return 23 + a[1]; case 1: return 24 + a[0]; case 2: delete a[2]; return a[0]+a[1]+a[3]; default: return el*el - 135; } }), r4 = [10, 11, 12].loop(function (el, i) { sum += el; if(i + 1 === this.length ){ return sum; } }), r5 = Array.prototype.loop.call("ABC", function(c) { return c.charCodeAt(0); }); </script>