Skip to content

Instantly share code, notes, and snippets.

@ethertank
Last active December 3, 2018 16:52
Show Gist options
  • Save ethertank/2420437 to your computer and use it in GitHub Desktop.
Save ethertank/2420437 to your computer and use it in GitHub Desktop.
Array
(function(AP, isF) {
'use strict';
// isArray : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
Array.isArray || (Array.isArray = function (a) {
return Object.prototype.toString.call(a) == "[object Array]";
});
// toSource(Non-standard)
AP.toSource || (AP.toSource = function() {
return this.slice();
});
// every : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
AP.every || (AP.every = function(f) {
if (!isF(f) || this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0, o = arguments[1], i = 0;
for (; i < l; ++i) if (i in t && !f.call(o, t[i], i, t)) return !1;
return !0;
});
// filter : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
AP.filter || (AP.filter = function(f) {
if (!isF(f) || this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0, r = [], T = arguments[1], i = 0;
for (; i < l; ++i) {
if (i in t) {
var v = t[i];
if (f.call(T, v, i, t)) r.push(v);
}
}
return r;
});
// forEach : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
AP.forEach || (AP.forEach = function(c, t) {
if (this == null) throw new TypeError("this is null or not defined");
if ({}.toString.call(c) !== "[object Function]") throw new TypeError(c + " is not a function");
var T, k, O = Object(this), l = O.length >>> 0;
if (t) T = t;
k = 0;
while (k < l) {
var v;
k in O && (v = O[k], c.call(T, v, k, O));
k++;
}
});
// map : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
AP.map || (AP.map = function(c, t) {
if (this == null) throw new TypeError(" this is null or not defined");
if ({}.toString.call(c) != "[object Function]") throw new TypeError(c + " is not a function");
var T = T ? t : undefined, k = 0, O = Object(this), l = O.length >>> 0, A = new Array(l);
while(k < l) {
var v, m;
k in O && (v = O[k], m = c.call(T, v, k, O), A[k] = m);
k++;
}
return A;
});
// reduce : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
AP.reduce || (Array.prototype.reduce = function reduce(a){
if (this === null || this === undefined) throw new TypeError("Object is null or undefined");
if(!isF(a)) throw new TypeError("First argument is not callable");
var i = 0, l = this.length >> 0, c;
if(arguments.length < 2) {
if (l === 0) throw new TypeError("Array length is 0 and no second argument");
c = this[0];
i = 1;
} else c = arguments[1];
while (i < l) {
if(i in this) c = a.call(undefined, c, this[i], i, this);
++i;
}
return c;
});
// reduceRight : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
AP.reduceRight || (AP.reduceRight = function(c){
if (!isF(c) || this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0;
if (l === 0 && arguments.length === 1) throw new TypeError();
var k = l - 1, a;
if (arguments.length >= 2) {
a = arguments[1];
} else {
do {
if (k in this) {
a = this[k--];
break;
}
if (--k < 0) throw new TypeError();
} while (true);
}
while (k >= 0) {
if (k in t) a = c.call(undefined, a, t[k], k, t);
k--;
}
return a;
});
// some : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
AP.some || (AP.some = function(f) {
if (!isF(f) || this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0, T = arguments[1], i = 0;
for (; i < l; ++i) if (i in t && f.call(T, t[i], i, t)) return !0;
return !1;
});
// indexOf : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
AP.indexOf || (AP.indexOf = function(e) {
if (this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0;
if (l === 0) return -1;
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (isNaN(n)) n = 0;
else if (n != 0 && n !== Infinity && n !== -Infinity) n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
if (n >= l) return -1;
var k = n >= 0 ? n : Math.max(l - Math.abs(n), 0);
for (; k < l; ++k) if (k in t && t[k] === e) return k;
return -1;
});
// lastIndexOf : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
AP.lastIndexOf || (AP.lastIndexOf = function(e) {
if (this == null) throw new TypeError();
var t = Object(this), l = t.length >>> 0;
if (l === 0) return -1;
var n = l;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) n = 0;
else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
for (var k = n >= 0 ? Math.min(n, l - 1) : l - Math.abs(n); k >= 0; --k) if (k in t && t[k] === e) return k;
return -1;
});
})(Array.prototype, function isF(o) { return typeof o === "function"; });
(function(b,a){Array.isArray||(Array.isArray=function(d){return Object.prototype.toString.call(d)=="[object Array]";});b.toSource||(b.toSource=function(){return this.slice();});b.every||(b.every=function(h){if(!a(h)||this==null){throw new TypeError();}var g=Object(this),d=g.length>>>0,j=arguments[1],e=0;for(;e<d;++e){if(e in g&&!h.call(j,g[e],e,g)){return !1;}}return !0;});b.filter||(b.filter=function(m){if(!a(m)||this==null){throw new TypeError();}var j=Object(this),d=j.length>>>0,k=[],g=arguments[1],h=0;for(;h<d;++h){if(h in j){var e=j[h];if(m.call(g,e,h,j)){k.push(e);}}}return k;});b.forEach||(b.forEach=function(j,h){if(this==null){throw new TypeError("this is null or not defined");}if({}.toString.call(j)!=="[object Function]"){throw new TypeError(j+" is not a function");}var g,f,i=Object(this),d=i.length>>>0;if(h){g=h;}f=0;while(f<d){var e;f in i&&(e=i[f],j.call(g,e,f,i));f++;}});b.map||(b.map=function(j,o){if(this==null){throw new TypeError(" this is null or not defined");}if({}.toString.call(j)!="[object Function]"){throw new TypeError(j+" is not a function");}var g=g?o:undefined,h=0,i=Object(this),f=i.length>>>0,d=new Array(f);while(h<f){var n,e;h in i&&(n=i[h],e=j.call(g,n,h,i),d[h]=e);h++;}return d;});b.reduce||(Array.prototype.reduce=function c(e){if(this===null||this===undefined){throw new TypeError("Object is null or undefined");}if(!a(e)){throw new TypeError("First argument is not callable");}var f=0,d=this.length>>0,g;if(arguments.length<2){if(d===0){throw new TypeError("Array length is 0 and no second argument");}g=this[0];f=1;}else{g=arguments[1];}while(f<d){if(f in this){g=e.call(undefined,g,this[f],f,this);}++f;}return g;});b.reduceRight||(b.reduceRight=function(h){if(!a(h)||this==null){throw new TypeError();}var g=Object(this),e=g.length>>>0;if(e===0&&arguments.length===1){throw new TypeError();}var f=e-1,d;if(arguments.length>=2){d=arguments[1];}else{do{if(f in this){d=this[f--];break;}if(--f<0){throw new TypeError();}}while(true);}while(f>=0){if(f in g){d=h.call(undefined,d,g[f],f,g);}f--;}return d;});b.some||(b.some=function(j){if(!a(j)||this==null){throw new TypeError();}var h=Object(this),d=h.length>>>0,e=arguments[1],g=0;for(;g<d;++g){if(g in h&&j.call(e,h[g],g,h)){return !0;}}return !1;});b.indexOf||(b.indexOf=function(h){if(this==null){throw new TypeError();}var g=Object(this),d=g.length>>>0;if(d===0){return -1;}var i=0;if(arguments.length>0){i=Number(arguments[1]);if(isNaN(i)){i=0;}else{if(i!=0&&i!==Infinity&&i!==-Infinity){i=(i>0||-1)*Math.floor(Math.abs(i));}}}if(i>=d){return -1;}var f=i>=0?i:Math.max(d-Math.abs(i),0);for(;f<d;++f){if(f in g&&g[f]===h){return f;}}return -1;});b.lastIndexOf||(b.lastIndexOf=function(h){if(this==null){throw new TypeError();}var g=Object(this),d=g.length>>>0;if(d===0){return -1;}var i=d;if(arguments.length>1){i=Number(arguments[1]);if(i!=i){i=0;}else{if(i!=0&&i!=(1/0)&&i!=-(1/0)){i=(i>0||-1)*Math.floor(Math.abs(i));}}}for(var f=i>=0?Math.min(i,d-1):d-Math.abs(i);f>=0;--f){if(f in g&&g[f]===h){return f;}}return -1;});})(Array.prototype,function isF(a){return typeof a==="function";});

たのしいはいれつ

length

配列の要素数を表す符号なし32ビット整数。

Array.length;

pop

配列の末尾の要素を削除する。

ArrayObj.pop();

push

配列の末尾に要素を追加する。

ArrayObj.push(追加要素);

shift

配列の先頭の要素を削除する。

ArrayObj.shift();

unshift

配列の先頭に要素を追加する。

ArrayObj.unshift(追加要素 [,追加要素[,...]]);

sort

配列の並び順を変更する。比較関数を省略した場合は文字コード順となる。

ArrayObj.sort([比較関数])

function 比較関数(x,y) { }

[10,40,30,20,50].sort(function(x,y) {
    return x - y;
});

reverse

配列要素の順序を反転する。

ArrayObj.reverse();

splice

配列を指定範囲で分割し、指定範囲の要素を返す。元の配列は、範囲外の要素からなる配列となる。
置換文字列が指定されていた場合は、指定範囲の後にこれが挿入される。

ArrayObj.splice(位置, 個数[repStr[,repStr[,...]]]);

concat

配列を連結する。

ArrayObj.concat(配列);

join

配列の要素を指定文字列で区切って文字列として連結する。

ArrayObj.join(区切り文字列);

slice

指定した範囲の配列要素を抜き出す。

ArrayObj.slice(start, end)

toSource

指定された配列を表す配列リテラルを返す。この値は新しい配列を作る際に使用可能。
Object.prototype.toSource の上書き。ES非標準。

ArrayObj.toSource();

toString

配列とその要素を表す文字列を返す。
Object.prototype.toString の上書き。

ArrayObj.toString();

indexOf

指定された値と等しい値を持つ最初の要素の添字を返す。見つからなかった場合は -1 を返す。

ArrayObj.indexOf(検索要素 [,検索開始位置]);

lastIndexOf

指定された値と等しい値を持つ最後の要素の添字を返す。見つからなかった場合は -1 を返す。

ArrayObj.lastIndexOf(検索要素 [,検索開始位置]);

isArray

与えられた変数が配列であるかどうかを調べる。

Array.isArray(ArrayObj);

every

配列要素全てがテスト関数を満たした場合にtrueを返す。
関数には「要素、位置、配列」が実引数として与えられる。

ArrayObj.every(テスト関数 [,thisObj]);

function テスト関数(elm, idx, arr) { }

var flg = [2,4,6].every(function(elm){
    return elm % 2 === 0;
});

some

ある配列の少なくとも1つの要素が与えられたテスト関数を満たした場合にtrueを返す。
関数には「要素、位置、配列」が実引数として与えられる。

ArrayObj.some(テスト関数 [,thisObj]);

function テスト関数(elm, idx, arr) { }

filter

与えられたフィルタリング関数がtrueを返す配列要素群のみからなる新しい配列を生成。
関数には「要素、位置、配列」が実引数として与えられる。

ArrayObj.filter(フィルター関数 [,thisObj])

function フィルター関数(elm, idx, arr) { }

    var evenArray = [0,1,2,3,4,5,6,7,8,9].filter(function(elm) { return elm % 2 !== 1; });

forEach

配列要素全てに対して関数を呼び出す。
関数には「要素、位置、配列」が実引数として与えられる。

ArrayObj.forEach(関数, [,thisObj]);

function 関数(elm, idx, array) { }

map

配列要素全てに対して関数を呼び出し、その結果からなる新しい配列を生成。
関数には「要素、位置、配列」が実引数として与えられる。

ArrayObj.map(関数 [,初期値]);

function 関数(prev, elm, idx, arr) { }

reduce

1つの値に還元されるように、配列の連続する2つの値に対し(左から右へ)同時に関数を適用していく。
関数には「前の要素、要素、位置、配列」が実引数として渡される。

ArrayObj.reduce(関数 [,初期値]);

function 関数(prev, elm, idx, arr) { }

reduceRight

1つの値に還元されるように、配列の連続する2つの値に対し(右から左へ)同時に関数を適用していく。
関数には「前の要素、要素、位置、配列」が実引数として渡される。

ArrayObj.reduceRight(関数 [,初期値]);

function 関数(prev, elm, idx, arr) { }

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" />
<script src="Array.js"></script>
<script src="test.js"></script>
</head><body><noscript>JavaScriptを有効にしておくれよ(・&forall;・)</noscript></body></html>
var viewResults = function(a) {
for(var i = 0; i < a.length;++i) { document.write(a[i] + "/"); }
document.write("<hr>");
a.length = 0;
};
var a = [0,1,2,3,4,5,6,7,8,9];
var b = [0,1,2,3,4,5,6,7,8,9,null];
var c = ["a","b","c","d","e","f","g"];
var d = [ [1,2,3], ["a","b","c"], ["x","y","z"] ];
var results = [];
// isArray
results = [
Array.isArray(a),
Array.isArray(b),
Array.isArray(c),
Array.isArray("x"),
Array.isArray(0),
Array.isArray({a:1}),
Array.isArray(null),
Array.isArray(undefined),
Array.isArray(function(s){ return s; })
];
viewResults(results);
// toSource
var myA = a.toSource();
var myD = d.toSource();
results = [
typeof myA,
myA instanceof Array,
myA,
a,
typeof myD,
myD instanceof Array,
myD,
myD[2],
myD[2].length,
d
];
viewResults(results);
// indexOf
results = [
c.indexOf("a"),
c.indexOf("a", -1),
c.indexOf("a", 1),
c.indexOf("a", 6),
c.indexOf("a", 100),
c.indexOf("g"),
c.indexOf()
];
viewResults(results);
// lastIndexOf
var results = [
c.lastIndexOf("a"),
c.lastIndexOf("a", -1),
c.lastIndexOf("a", 1),
c.lastIndexOf("a", 6),
c.lastIndexOf("g", 100),
c.lastIndexOf()
];
viewResults(results);
// every
results = [
a.every(function(e) {
return (typeof e === "number");
}),
b.every(function(e) {
return (typeof e === "number");
})
];
viewResults(results);
// some
results = [
a.some(function(e) {
return (e === 1);
}),
a.some(function(e) {
return (e > 10);
})
];
viewResults(results);
//filter
results = [
a.filter(function(elm) {
return elm % 2 !== 1;
}),
a.filter(function(elm) {
return elm < 6;
})
];
viewResults(results);
document.write(a + "<hr>");
// forEach
a.forEach(function(e,i,a) {
document.write(e + " ");
});
document.write(" / ");
a.forEach(function(e,i,a) {
document.write(i + " ");
});
document.write("<hr />");
// map
results = [
a.map(function(elm) {
return elm * 10;
}),
a.map(function(elm, idx) {
return elm + idx;
})
];
viewResults(results);
document.write(a + "<hr>");
// reduce
results = [
a.reduce(function(prev, curr, idx, arr){
return prev + curr;
}),
a.reduce(function(prev, curr, idx, arr){
return curr - prev * idx;
}),
a.reduce(function(prev, curr, idx, arr){
return idx + arr[idx];
})
];
viewResults(results);
// reduceRight
results = [
a.reduceRight(function(prev, curr, idx, arr){
return prev + curr;
}),
a.reduceRight(function(prev, curr, idx, arr){
return prev - curr;
}),
a.reduceRight(function(prev, curr, idx, arr){
return prev + idx;
})
];
viewResults(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment