Skip to content

Instantly share code, notes, and snippets.

@wohugb
Last active August 29, 2015 14:12
Show Gist options
  • Save wohugb/17082aa768ca9fe1526f to your computer and use it in GitHub Desktop.
Save wohugb/17082aa768ca9fe1526f to your computer and use it in GitHub Desktop.
js数组
//JS在1.6中为Array新增了几个方法map(),filter(),some(),every(),forEach()
//@ref http://www.cnblogs.com/xiao-hong/p/3194027.html
map()//:返回一个新的Array,每个元素为调用func的结果
filter()//:返回一个符合func条件的元素数组
some()//:返回一个boolean,判断是否有元素是否符合func条件
every()//:返回一个boolean,判断每个元素是否符合func条件
forEach()//:没有返回值,只是针对每个元素调用func
//测试是否所有数组元素都大于等于10:
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
//结果:
//[12, 5, 8, 130, 44].every(isBigEnough) 返回 : false
//[12, 54, 18, 130, 44].every(isBigEnough) 返回 : true
/*
* 内部实现:
*/
Array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this && !fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
/*
* 用法:
* 参数说明:
* callback: 要对每个数组元素执行的回调函数。
* thisObject : 在执行回调函数时定义的this对象。
*/
var filteredArray = array.filter(callback[, thisObject]);
/*
* 示例:
* 过滤掉小于 10 的数组元素
* 输出:12, 130, 44
*
*/
function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
/*
* 内部实现:
*/
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
//打印数组内容:
function printElt(element, index, array) {
document.writeln("[" + index + "] is " + element + "<br />");
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9
//结果:
//[0] is 2
//[1] is 5
//[2] is 9
/*
* 内部实现:
*/
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
/查找符合条件的元素:
var array = [2, 5, 9];
var index = array.indexOf(2);
// index is 0
index = array.indexOf(7);
// index is -1
//结果:
//[2, 5, 9].indexOf(2) : 0
//[2, 5, 9].indexOf(7) : -1
//查找符合条件的元素:
var array = [2, 5, 9, 2];
var index = array.lastIndexOf(2);
// index is 3
index = array.lastIndexOf(7);
// index is -1
index = array.lastIndexOf(2, 3);
// index is 3
index = array.lastIndexOf(2, 2);
// index is 0
index = array.lastIndexOf(2, -2);
// index is 0
index = array.lastIndexOf(2, -1);
// index is 3
//结果:
//[2, 5, 9, 2].lastIndexOf(2) : 3
//[2, 5, 9, 2].lastIndexOf(7) : -1
//[2, 5, 9, 2].lastIndexOf(2, 3) : 3
//[2, 5, 9, 2].lastIndexOf(2, 2) : 0
//[2, 5, 9, 2].lastIndexOf(2, -2) : 0
//[2, 5, 9, 2].lastIndexOf(2, -1) : 3
//将所有的数组元素转换为大写:
var strings = ["hello", "Array", "WORLD"];
function makeUpperCase(v)
{
return v.toUpperCase();
}
var uppers = strings.map(makeUpperCase);
// uppers is now ["HELLO", "ARRAY", "WORLD"]
// strings is unchanged
//结果:["hello", "Array", "WORLD"].map(makeUpperCase) : HELLO, ARRAY, WORLD
/*
* 内部实现:
*/
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
//检查是否有数组元素大于等于10:
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true
//结果:
//[2, 5, 8, 1, 4].some(isBigEnough) : false
//[12, 5, 8, 1, 4].some(isBigEnough) : true
/*
* 内部实现:
*/
Array.prototype.some = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this && fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment