In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of array objects in JavaScript", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of array objects in JavaScript".
Array object
Classification of arrays *
1. A two-dimensional array. The essence of a two-dimensional array is that the elements in the array are also arrays.
Var arr = [[1mem2], [aQuinb]]; alert (arr [1] [0]); / / element in the first row of column 2
2. Sparse array
A sparse array is an array that contains discontiguous indexes starting at 0. In sparse arrays, the value of the length attribute is generally larger than the actual number of elements (uncommon)
Give an example
Var a = ["a", "b", "c",]
Array object Properties
Attribute functions the length attribute to represent the length of the array, that is, the number of elements in which the prototype attribute returns a reference to the object type prototype, the constructor attribute represents the function for creating the object
1.length attribute:
Alert (arr.length); / / the length of the display array is 10arr.length 15; / / increase the length of the array, the length attribute is variable alert (arr.length); / / the length of the display array has changed to 15
2.prototype attribute
The prototype property returns a reference to the prototype of the object type. The prototype attribute is common to object.
ObjectName.prototype
The objectName parameter is the name of the object object.
Description: use the prototype property to provide a set of basic functions of the object's class. A new instance of the object "inherits" the operation assigned to the object prototype.
For array objects, use the following example to illustrate the purpose of the prototype property.
Add a method to an array object that returns the largest element value in the array. To do this, declare a function, add it to Array.prototype, and use it.
Function array_max () {var I, max = this [0]; for (I = 1; I
< this.length; i++){ if (max < this[i]) max = this[i]; } return max;}Array.prototype.max = array_max;var x = new Array(1, 2, 3, 4, 5, 6);var y = x.max( ); 3.constructor 属性 constructor 属性表示创建对象的函数。 object.constructor //object是对象或函数的名称。 说明:constructor 属性是所有具有prototype 的对象的成员。它们包括除 Global 和 Math对象以外的所有JScript固有对象。constructor属性保存了对构造特定对象实例的函数的引用。 例如: x = new String("Hi");if (x.constructor == String) // 进行处理(条件为真)。//或function MyFunc {// 函数体。}y = new MyFunc;if (y.constructor == MyFunc) // 进行处理(条件为真)。 Array的对象方法 说明:部分是ECMAScript5的新特性(IE678不支持) 方法作用concat()连接两个或者更多的数组,并返回结果join()将数组的元素组起一个字符串pop()删除并返回数组的最后一个元素push()数组末尾添加一个或者多个元素,返回新的长度reverse颠倒数组中元素的顺序shift()删除并返回数组的第一个元素slice()从某个已有的数组返回选定的元素sort()对数组元素排序splice()删除元素,并向数组添加新元素toSource()返回该对象的源代码toString()把数组转化为字符串并返回结果toLocalString()把数组转化为本地元素并返回结果unshift向数组开头添加一个或者更多的元素,并返回新的长度valueof()返回数组对象的原始值forEach()遍历数组对象map()对数组做一些映射filter()过滤every()检查判断some()检查判断reduce()两两执行一定的操作reduceRight()从右到左执行操作indexOf()数组检索查找某个元素Array.isArray([])判断是否是数组 主要对一些新特性进行讲解 concat concat作用是拼接数组,需要注意的是也可以把一个数组元素作为拼接的元素,如果这样的话,数组会被拉平,再和其它的元素拼接起来成为新的数组,但是不会被拉平两次,concat不会修改原数组。 例如: var arr=[1,2,3,4,5];arr.concat([10,11],13);//[1,2,3,4,5,10,11,13]arr.concat([1,[2,3]]);//[1,2,3,4,5,1,[1,3]] slice slice(a,b)a和b可以取负数,表示从a位置开始截取到b位置的一段数组,是一个左闭右开的区间,a和b可以取负数,如果是负数代表倒数第a/b个元素 var arr=[1,2,3,4,5];arr.slice(1,3);//[2,3]arr.slice(1);//[2,3,4,5]arr.slice(1,-1);//[2,3,4]arr.slice(-4,-3);//[2] splice splice删除元素并向数组添加新元素 object.splice(a)从左边开始删除a个元素 object.splice(a,b)从a位置开始截取其中的b个元素 object.splice(a,b,c,d)从a位置开始截取b个元素,并将c和d或者更多的元素插入原数组 需要注意的是splice会修改原数组 var arr=[1,2,3,4,5];arr.splice(2);//[3,4,5]arr;//[1,2];原数组被修改了var arr=[1,2,3,4,5];arr.splice(2,2);//[3,4]arr;//[1,2,5];var arr=[1,2,3,4,5];arr.splice(1,1,'a','b');//[2]arr;//[1,"a","b",3,4,5]; foreach foreach()函数从头到尾把数组遍历一遍。有三个参数分别是:数组元素,元素的索引,数组本身 var arr = [1, 2, 3, 4, 5];arr.forEach(function(x, index, a){//分别对应:数组元素,元素的索引,数组本身 console.log(x + '|' + index + '|' + (a === arr));});// 1|0|true// 2|1|true// 3|2|true// 4|3|true// 5|4|true reduce() Array的reduce()把一个函数作用在这个Array的[x1, x2, x3…]上,这个函数必须接收两个参数,reduce()把结果继续和序列的下一个元素做累积计算,其效果就是: [x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)var arr = [1, 2, 3];var sum = arr.reduce(function(x, y) { return x + y}, 0); //参数 0是可选的,如果写了参数0那第一次传递的两个值就是0和1如果不写第一次传递的就是数组的前两个值,计算结果是6arr; //[1, 2, 3]arr = [3, 9, 6];var max = arr.reduce(function(x, y) { console.log(x + "|" + y); return x >Y? X: y;}); / / 3 | 9 gamma / 9 | 6max; / / 9
Comparison between array and general object
Array / general object similarities can be inherited, objects are not necessarily arrays, can be used as objects to add attributes different point array automatic update length index access array is obviously faster than general object properties. Array objects inherit a large number of array manipulation methods on Array.prototype
Comparison of arrays and strings
Array / string identical point string is a different point of an array A string is an immutable array, and a string has no array.
The above is all the content of the article "sample Analysis of Array objects in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.