zoukankan      html  css  js  c++  java
  • Array对象方法属性汇总

    创建数组

    一、采用直接量创建

    	var arr1 = [];   			     //创建一个空数组
    	var arr2 = [1, 2, 3];      // 创建一个有三个元素的数组
    

    二、采用构造函数创建

    	var arr3 = new Array();    //创建一个空数组
    	var arr4 = new Array(10);  //创建一个长度为10的空数组
    	var arr5 = new Array(5,4,3,2,1);  //创建数组并初始化
    

    数组属性

    一、constructor (返回创建数组对象的原型函数)

    	var cars = ['Volvo', 'BMW', 'Saab'];
    	cars.construtor; 
    	//输出:function Array() { [native code] }
    

    二、length (返回数组的长度,即数组元素的个数)

    	var cars = ['Volvo', 'BMW', 'Saab'];
    	cars.length;
    	//输出:3
    	//cars含有3个元素,所以cars的长度为3       
    

    三、prototype (Array属性构造器,为原型函数Array添加属性或方法)

    	Array.prototype.myUpperCase = function() {
    		for(i=0; i<this.length; i++) {
    			this.[i] = this.[i].toUpperCase();
    		}
    	};
    
    	var fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
    
    	fruits.myUpperCase();
    
    	console.log(fruits);
    
    	//输出: ['BANANA', 'ORANGE', 'APPLE', 'MANGO'];
    
    

    Array对象属性

    一、every()方法

    1、定义和用法

    • every()方法用于检测所有元素是否都符合指定条件(通过函数提供)
    • every()方法使用指定函数检测数组中的每一个元素

    注意:every()方法不会对空数组进行检测
    注意:every()方法不会改变原数组

    2、语法

    	array.every(function(currentValue, index, arr), thisValue);
    	
    	/**
    	 * @ function(currentValue, index, arr)
    	 *   必须。函数,数组中的每个元素都会执行这个函数
    	 *   @ currentValue: 必须,当前元素的值
    	 *   @ index:可选,当前元素的索引
    	 *   @ arr:可选,当前元素属于的数组对象
    	 *
    	 * @ thisValue: 可选,对象作为该执行回调时使用,传递给函数,用作‘this’的值,如果省略了thisValue,‘this’的值为‘undefined’
    	 */
    	
    	// 返回值:布尔值,如果所有元素都通过检测则返回true,否则返回false。
    
    

    3、实例

    	var numbers = [2, 3, 4, 5, 7, 8];
    
    	numbers.every(function(curr) {
    		return curr >= 5;
    	});
    
    	// 上边代码的返回值为false, 因为存在小于5的值
    
    	numbers.every(function(curr) {
    		return curr >= 1;
    	});
    
    	// 上面代码的返回值为true, 数组中每一个元素都大于1
    
    

    二、some()方法

    1、定义和用法

    • some()方法用于检测数组中的元素是否满足指定条件
    • some()方法会依次执行数组的每个元素

    注意:some()方法不会对空数组进行检测
    注意:some()方法不会改变原数组

    2、语法

    	array.some(function(currentValue, index, arr), thisValue);
    
    	/**
    	 * @ function(currentValue, index, arr)
    	 *   必须。函数,数组中的每个元素都会执行这个函数
    	 *   @ currentValue: 必须,当前元素的值
    	 *   @ index:可选,当前元素的索引
    	 *   @ arr:可选,当前元素属于的数组对象
    	 *
    	 * @ thisValue: 可选,对象作为该执行回调时使用,传递给函数,用作‘this’的值,如果省略了thisValue,‘this’的值为‘undefined’
    	 */
    	
    	// 返回值:布尔值,如果数组中有元素满足条件则返回true, 否则返回false;
    
    

    3、实例

    	var ages = [4, 12, 16, 20, 24];
    	ages.some(function(curr) {
    		return curr >= 18;
    	});
    
    	// 上面代码的返回值为true,因为存在大于等于18的值
    
    

    三、map()方法

    1、定义和用法

    • map()方法返回一个新数组,数组中的元素为原始数组元素调用处理后的值
    • map()方法按照原始数组元素顺序依次处理元素

    注意:map()不会对空数组进行检测
    注意:map()不会改变原始数组

    2、语法

    	array.map(function(currentValue, index, arr), thisValue)
    
    	/**
    	 * @ function(currentValue, index, arr)
    	 *   必须。函数,数组中的每个元素都会执行这个函数
    	 *   @ currentValue: 必须,当前元素的值
    	 *   @ index:可选,当前元素的索引
    	 *   @ arr:可选,当前元素属于的数组对象
    	 *
    	 * @ thisValue: 可选,对象作为该执行回调时使用,传递给函数,用作‘this’的值,如果省略了thisValue,‘this’的值为‘undefined’
    	 */
    	
    	// 返回值:返回数组,数组中的元素为原始数组元素调用函数处理后的值
    
    

    3、实例

    	var names = ['Edison', 'Daniel', 'Jack', 'Andy'];
    
    	names.map(function(curr) {
    		return curr.toUpperCase();
    	})
    
    	// 输出:['EDISON', 'DANIEL', 'JACK', 'ANDY'];
    
    

    四、forEach()方法

    1、定义和用法

    • forEach()方法会遍历数组中的每个元素,并将元素传递给回调函数

    注意:forEach()对于空数组不会执行回调函数
    注意:forEach()也不会改变原始数组

    2、语法

    	array.forEach(function(currentValue, index, arr), thisValue);
    
    	/**
    	 * @ function(currentValue, index, arr)
    	 *   必须。函数,数组中的每个元素都会执行这个函数
    	 *   @ currentValue: 必须,当前元素的值
    	 *   @ index:可选,当前元素的索引
    	 *   @ arr:可选,当前元素属于的数组对象
    	 *
    	 * @ thisValue: 可选,对象作为该执行回调时使用,传递给函数,用作‘this’的值,如果省略了thisValue,‘this’的值为‘undefined’
    	 */
    
    	// 返回值:undefined,
    

    3、实例

    	var sum = 0;
    	var numbers = [1, 2, 3, 4, 5];
    
    	numbers.forEach(function(curr) {
    		sum += curr;
    	});
    
    	console.log(sum);  // 15
    
    	// 遍历数组numbers中的每个元素,并将当前元素与sum相加。
    
    

    五、filter()方法

    1、定义和用法

    • filter()方法会返回一个新的数组,新数组中的元素是符合条件函数检查的元素。

    注意:filter()不会对空数组进行检测
    注意:filter()不会改变原始数组

    2、语法

    	array.filter(function(currentValue, index, arr), thisValue)
    
    	/**
    	 * @ function(currentValue, index, arr)
    	 *   必须。函数,数组中的每个元素都会执行这个函数
    	 *   @ currentValue: 必须,当前元素的值
    	 *   @ index:可选,当前元素的索引
    	 *   @ arr:可选,当前元素属于的数组对象
    	 *
    	 * @ thisValue: 可选,对象作为该执行回调时使用,传递给函数,用作‘this’的值,如果省略了thisValue,‘this’的值为‘undefined’
    	 */
    	
    	// 返回值:返回数组(符合条件的元素组成的新数组)。如果没有符合条件的元素则返回空数组
    
    

    3、实例

    	var ages = [18, 24, 30, 23, 12];
    
    	ages.filter(function(curr) {
    		return curr >= 18;
    	});
    
    	// 输出:[18, 24, 30, 23]
    
    
  • 相关阅读:
    mysql 下 计算 两点 经纬度 之间的距离
    富爸爸财务自由之路
    Ubuntu16.04忘记MySQL5.7的root用户密码之解决方案
    Windowns下code: command not found
    Linux下CRMEB环境搭建
    PHP无法使用curl_init()函数
    请在mysql配置文件修sql-mode或sql_mode为NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    apache不能解析php之解决办法
    windows上hexo: command not found
    SyntaxError: Non-ASCII character 'æ' in file csdn.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
  • 原文地址:https://www.cnblogs.com/jayxiangnan/p/9072797.html
Copyright © 2011-2022 走看看