JavaScript basic (5) - Array

  We all know typeof (Array) is a calculated value object, in JavaScript, the array is generally a set of consecutive indexes and values, and a set of objects is often uncertain attributes and values, their essential difference is that property values are continuous ( Note: the array elements may be discontinuous, as sparse array ). In programming, a group of objects has a continuous index value has many advantages: First, when using an array of users do not care about the property name, because the index values are continuous auto-incremented integer. Secondly, when users access the array can be easily traversed, this can be done for loop. As for the JavaScript engine, the array object is assigned a set of consecutive expandable memory when it is created, due to the continuity of memory allocation, the array can be completed faster indexing, that is, an array of objects often have more than good access performance.

  In summary, the array is a special form of the object. So why the concept of an array, you need to carry out a separate na? Think: in any programming language, one of the array are essential data type, JavaScript is no exception, when we use an array to create an array of objects directly amount, it will inherit a lot of useful array operations from the Array.prototype methods, these methods of operation so that an array of real objects and distinguished.

1. Create an array

  Method creates an array of mainly two types of array literals, another is to call the constructor to create the array, as follows

let arr = [] //创建一个空数组
let arr2 = new Array(10) //第一个参数指定创建数组的长度,但是数组中没有存储值

  Note that, arr2 is just an array of a specified length (arr.length: 10), which has no value is stored in the array, the array index value is even not defined.

2. The array of read and write

  And reading and writing of the same array of objects. Note that the following special cases:

  1. You can not point (.) Operator to access array elements, not as an integer value because the computation of the dot operator, so we need square brackets ([]) to access array elements.

  2. The array index starts from 0 in general, but not absolute, a [-1] = 1 creates a named attribute value of -1, as far as possible not to do so.

  3. The array as the object can be inherited from the prototype elements, element arrays getter and setter methods can be defined.

3. sparse array

  Sparse array length property is greater than the number of elements in the array. You can directly create a sparse array in the constructor.

let arr = new Array(5) //创建一个length值为5,数组元素为0的元素

 In addition to the constructor, the amount of direct objects can also be done to create a sparse array.

let a = []
a[1000] = 0 //length为1001,但是只有一个元素

 The above two more conventional methods, in addition, you can also use the delete operator to generate a sparse array. Here, however much introduction. In the creation of the sparse array to note the following two cases.

let arr1 = [,,,] // [undefined,undefined,undefined]
let arr2 = new Array(3) // 该数组没有任何元素
0 in arr1 //true
0 in arr2 //false

4. The length of the array

  Each array has a length property, this property is different from the normal JavaScript objects. The length property of the array is the support manual settings.

  If you set the length property of the array is greater than its current length, then it will create an empty area in the tail.

  If you set the length property of the array is smaller than its current length, so that he would be greater than or equal to the index n elements removed from the array. It can be understood as, length is the maximum array index value if the index value is greater than the length of the array element value, and that he would not be indexed to that is "deleted."

  Since the length value to modify the array will produce some unexpected situation, so we can use Object.defineProperty () allows the array length becomes read-only attribute.

let arr = [1,2]
Object.defineProperty(arr,'length',{writable:false})

5. The method of array common

  In the beginning of the article, I mentioned, the biggest difference is that arrays and objects, arrays inherited from Array.prototype, and objects inherited from Object.prototype, an array of inherited many operating functions from the Array object, let's first take a look at a list Some commonly used functions, you can find specific usage and parameters for each function explained later.

<-- 数组元素的添加 -->
arr.push(val)   //在数组的尾部添加一个值,返回该元素,并修改数组本身
arr.unshift(val)  //在数组的头部添加一个值,返回该元素,并修改数组本身
arr.splice(1,0,2) //第一个参数指定了插入(删除)元素的起始位置,第二个参数指定了个数
//第二个之后的参数数量为任意个,指定了需要插入到数组中的元素

<-- 数组元素的删除 -->
arr.pop() //删除数组的最后一个元素,返回该元素,并修改数组本身
arr.shift() //删除数组头部的元素,返回该元素,并修改数组本身
arr.splice(1,2) //表示删除元素的从第‘1’个开始,到第‘2’个结束,也就是删除了第二个和第三个元素
arr.slice(2,3) //两个参数指截取数组开始和结束的字段,
//如果第二个值是负整数,则代表倒数第几个元素,如果第二个值缺省,则代表到最后一个元素
//该方法返回截取的片段,并且不会修改调用的数组

<-- 数组元素的排序 -->
arr.sort((a,b)=>{return (a-b)}) //默认按字母表顺序排,a-b从小到大排,b-a从大到小

<-- 数组元素的遍历 -->
arr.forEach((item)=>{}) //该方法会自动过滤掉空值
arr.map((item)=>{return item+1}) //该方法会返回一个新的数组,并不会修改原数组
arr.fiter((item)=>{return item!==1})//该方法会返回一个索引值符合回调函数判断的新数组

<-- 数组元素的逻辑判定 --> 
arr.every((item)=>{return item===1})//判定数组中的每个元素是否符合回调函数里的判断,返回一个布尔值,遇到false就终止循环
arr.some((item)=>{return item===1})//判定数组中是否有元素符合回调函数里的判断,返回一个布尔值,遇到true就终止循环

<-- 连接两个数组 -->
arr.concat([1,2,3]) //该方法返回一个新数组,对原数组没有影响,所以需要一个新的数组接收值

<-- 数组转字符串 -->
arr.join('/') //括号内指定元素之间的分隔符,默认是逗号,该方法是String.split()的逆向操作

<-- 数组元素倒序 -->
arr.reverse() //该方法把数组中的元素颠倒,如果面试考这个,可以直接写

<-- 数组元素的组合 -->
arr.reduce((total,current)=>{
    return total+current
},0)
//reduce本意是缩小,减少,却经常被拿来做加法运算,后面我会详细介绍这个方法

<-- 数组元素的查询 -->
arr.find((item)=>{return item===1}) //查找到第一个符合条件的值,并返回该值
arr.indexOf((item)=>{return item===1}) //查找到第一个符合条件的值,并返回索引值

  

Published 109 original articles · won praise 196 · Views 300,000 +

Guess you like

Origin blog.csdn.net/dkr380205984/article/details/99844622