Js data type - Array

Array Introduction

In the Java Script language (hereinafter referred to as Js), divided into basic data types (simple) data types and reference (complex) data types, which we commonly used as a reference data types - arrays, then we take a look at the array some of the basics.

Array basis

Array (Array), it is the most common computer language data structures, but also the JS, the importance of an array of one of the most common data structure is self-evident. An array is an ordered collection of data. In Js, each value of the array becomes an element of the array, the characteristics of Js in the array are the same elements in the array can be of any type (Js is weakly typed language), but usually we save only the value of the same data type in the same array ; number of elements in the array is called the length of the array (length), an array of length 0 is called empty array, each array element has can be accessed by a number, it referred to as an index (or index) array index starts from 0, the index of the last element in the array should be a length of the array.

Array creation

Create an array fashion it can be divided into the amount of plane to create and use an array constructor to create an array of two ways.
1, literal creation:

let arr=[];

2, a constructor argument:

let arr=new Array();

Array assignment

To array assignment method is very simple and can be divided into direct assignment when the first re-assignment statement and statements:
the first statement by assignment:

let arr = [];
arr[0] = 1;
Arr [1] = 2;

When the direct assignment statement:

let arr = -[3 ,4];
let arr1 = new Array(3,4);

We can assignment anywhere in the array, the length of the actuator will automatically change the group, the position of the empty because there is no use, may all be considered undefined.

Access array elements (check)

By array index (subscript), we can easily access to the inside of the elements stored in the array, as follows:

let arr = [1,2,3,4,5];
console.log(arr[0]): //1

Again: The first element of the array from inside the index is zero-based.

Remove elements (deleted)

We can use the delete operator to delete a certain element in the array, for example:

let arr = [1,2,3,4,5];
delete arr[2]//删除数组中的第3个元素
console.log(arr);//[ 1, 2, <1 empty item>, 4, 5 ]

Of course, we can say that this method is very rare, follow-up will be talked about listening to splice to delete array elements.

Length of the array

Length of the array, i.e. the number of Js saved in the array provides a data length attribute array, or returns to set the length of the array.
Syntax: array name .length
example:

let arr = [1,2,3,4,5];
console.log(arr.length);//输出数组的长度5

We can remove all of or behind elements of the array elements of array length by forcibly setting
Example:

let arr = [1,2,3,4,5];
arr.length=2;
console.log(arr);//输出arr数组目前的所有元素[ 1, 2 ];
arr.length=0;
console.log(arr);//删除后arr的内容清空,变为空数组

Iterate

The role of the array can save multiple batch of data, but when we need to operate on the data, and can not directly operate the array to achieve the effect, still need to be taken out of each piece of data from the array, and then in turn take them operating. We used an array traversal follows:
. 1, for loop by loop through the array of elements each
embodiment,

let arr = [1,2,3,4,5];
for(let i = 0;i<arr.length;i++){
    console.log(arr[i]);
}

: 2, and the for-in circulation loop for-use of
for ES6 added a method for-of one kind through the array, from the comparison / or its cyclic code for more concise.
example,

let arr = [1,2,3,4,5];
for(let i of arr){//i就代表数组的一项
    console.log(i);
}

In addition we can also take advantage of the underlying ways to traverse each element of the array to get at for-in loop through the array:
cases,

let arr = [1,2,3,4,5];
for(let i in arr){//i代表数组的下标(索引)
    console.log(arr[i]);
}

An array of common methods

Array contains a number of methods for manipulating arrays. This introduces some of the more common methods:
1, delete data, delete, and into the last undeleted from the very beginning:
to delete from the start:

let arr = [1,2,3,4,5];
arr.shift();
console.log(arr);//输出arr数组目前删除元素后的数组[ 2, 3, 4, 5 ]

Delete from behind:

let arr = [1,2,3,4,5];
arr.pop();
console.log(arr);//输出arr数组目前删除元素后的数组[ 1, 2, 3, 4 ]

2, increase data, divided from the very beginning to increase and not the last increase:
increase from the end of

let arr = [1,2,3,4,5];
arr.push(6);
console.log(arr);//输出arr数组目前增加元素后的数组[ 1, 2, 3, 4, 5, 6 ]

Increases from

let arr = [1,2,3,4,5];
arr.unshift(6);
console.log(arr);//输出arr数组目前增加元素后的数组[ 6, 1, 2, 3, 4, 5 ]

Js provides a universal method for any location of the data array to add or remove, the following syntax structure:
array name .splice (subscript, delete the number, according to a new actuator, new data 2)
Note: new data can be any number, including zero, if you do not delete the data only need to add, delete the number 0 can be written:

let arr = [1,2,3,4,5];
arr.splice(1,1);//下标为1的位置删除一个数并且不添加元素
console.log(arr);//输出arr数组目前删除元素后的数组[ 1, 3, 4, 5 ]

Find data

Find an index data, common methods:
the indexOf actuator according to find the first occurrence of the subscript;
lastlndex0f (): Find the last occurrence of the subscript data:
if the corresponding value found in the array to give the corresponding index is returned, and immediately stop the search, if the data is not present in the array to change the return -1;
Example

let arr = [1,2,3,4,5];
console.log(arr.indexOf(2));//输出元素在数组中的下标 1
console.log(arr.lastIndexOf(2));//输出元素在最后一次出现在数组中的下标 1
console.log(arr.indexOf(20));//元素不存在,返回 -1

Another method to find whether an existing data Includes (), by returning true or false value to determine whether there:
Example,

let arr = [1,2,3,4,5];
console.log(arr.includes(20));//false 不存在
console.log(arr.includes(1));//true 存在

Note: Because NaN is not equal to any number, including its own, so if you need to use NaN includes methods, the method does not exist query using indexOf and lastIndexOf array.
Find whether there is some data:
Js provided slice method used to find part of the data array, data and find the composition of a new array.
.Slice array name (subscript start position, end position index), is used to find all data between Slice from a start position to an end position (not included).

let arr = [1,2,3,4,5];
console.log(arr.slice(2,4));//查找数组中下标为2到下标为4(不包括)之间的数据并返回 得到新数组 [ 3, 4 ]

Sorting data

Js provides a set of methods, all data in the sequence of operation for the array.
reverse: all the data in reverse order (reverse order considerations not reverse output);
Example

let arr = [6, 2, 8, 1, 5];
console.log(arr.reverse());//点到顺序并返回新数组 [ 5, 1, 8, 2, 6 ]

sort: sort all data in accordance with a given condition;
Example

let arr = [6, 2, 8, 1, 5];
console.log(arr.sort((a, b) => a - b)); //升序排序并返回新数组 [ 1, 2, 5, 6, 8 ]
console.log(arr.sort((a, b) => b - a)); //降序排序并返回新数组 [ 8, 6, 5, 2, 1 ]

Array and string conversion

Js is provided a method of split between the user and Strings conversion ( "") and join ( "").
split ( "") method is used to convert a string array, the brackets which fill separator, the separator can be customized;
Example

let str = "1,5,8,1,3";
let arr = str.split(",");
console.log(arr);//得到转换成功后的数组 [ '1', '5', '8', '1', '3' ]

join ( "") method
Example

let str = "1,5,8,1,3";
let arr = str.split(",");
console.log(arr);//得到转换成功后的数组 [ '1', '5', '8', '1', '3' ]
console.log(arr.join(""));//得到转换成功后的字符串 "15813"

[Extensions] Other methods of array traversal

In addition to previously mentioned for, for-in and for-of loop to loop through the array, we Js iterate But there are other methods, but we are also very popular;
forEach () method;
cases,

let arr = [6, 2, 8, 1, 5];
arr.forEach((value)=>{
    console.log(value);//遍历得到爱arr的每一项
});

It comes forEach loop array, the use of higher frequencies, but the performance is actually weaker than normal cycle for.
forEach not use continue and break statement break the cycle, you can not use the return statement to return to the outer function.
map () method: in order to let each array sequentially perform some of the same operation, and then returns a new array.
example,

let arr = [6, 2, 8, 1, 5];
arr.forEach((value)=>{
    console.log(value);//遍历得到爱arr的每一项
});

Summary: forEach difference () and the map (), the former is no return value, after which a new array will be the operation returns.
In addition there is an array of every (), some () and filter () method we more commonly used:
1, Every () method to determine whether each item in the array to meet the conditions, returns true if the whole meet, as long as at least one not satisfied return false:
cases,

let arr = [6, 2, 8, 1, 5];
let re = arr.every((value)=>{
   return value >= 1;
});
console.log(re);//由于数组每一项都大于或等于1,因此返回true
let re1 = arr.every((value)=>{
    return value >= 2;
 });
 console.log(re1);//由于数组不满足每一都大于2,因此返回false

some () method, the array is determined whether any of the at least one condition is satisfied immediately returns true and that satisfies the termination functions, if all is not satisfied it returns false if found;
Example

let arr = [6, 2, 8, 1, 5];
let re = arr.some((value)=>{
   return value >= 3;
});
console.log(re);//数组中存在大于3的数据,因此返回true
let re1 = arr.every((value)=>{
    return value >= 10;
 });
 console.log(re1);//数组中不存在大于10的数,因此返回false

the filter (), it is determined whether the condition of each one separately, and the return value satisfies the condition, so we used to do the screening filter data.
example,

let arr = [6, 2, 8, 1, 5];
let re = arr.filter((value)=>{
   return value >= 3;
});
console.log(re);//筛选出满足条件的值并返回 [ 6, 8, 5 ]

Summary: Js is a weakly typed language, so the array can store any type of data, of course, we use every day are more the same type of data, Js position in the array can be described as pivotal in the future of all of us still learning heavy and long road.

Published 19 original articles · won praise 0 · Views 298

Guess you like

Origin blog.csdn.net/Joey_Tribiani/article/details/103301682