Common methods for arrays
Arrays as an important data type, in addition to the pop, push, shift, and unshift several methods we have already mentioned, there are many practical methods that are also essential to our skills.
Suppose we have a team of people, such as:
We want to do some sort or filter operations (metaphor by height, filter women, etc.), and we can all use arrays to manipulate them.
Note: Here is more emphasis on how to use, as for detailed methods please refer to: Arrays | MDN
Pull out some people
First we use an array to define the data (for simplicity, we don't have that much data):
var aPerson = [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]
Slice
Now suppose we want to extract three people, we can use slice() the method to select three people, as follows:
var aP3 = aPerson.slice(1, 4);console.log(aPerson); // [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]console.log(aP3); // ["person2", "person3", "person4"]
The method returns a shallow copy of the selected array from start to finish (excluding end) to a new array object. The original array does not change.
For detailed syntax, please refer to: Slice
Splice
Also we can use splice() the method to select, as follows:
var aPerson = [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]var aP3 = aPerson.splice(1, 3);console.log(aPerson); // ["person1", "person5", "person6"]console.log(aP3); // ["person2", "person3", "person4"]
The method changes the contents of an array by deleting an existing element or adding a new element. The original array will change.
For slice, the splice function will be more powerful, the difference is mainly:
- Slice does not change the original array, while splice will change
- The second parameter of slice is the index value up to, while splice indicates the number to intercept
- Splice can also be used to add elements, slice can not
For detailed syntax, please refer to: Splice
Concat
In addition to pulling out some people out of the team, we can also combine another team and this team into a new team, as follows:
var aPerson1 = [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]var aPerson2 = [‘person7‘, ‘person8‘, ‘person9‘];var aPerson3 = aPerson1.concat(aPerson2);console.log(aPerson3); // ["person1", "person2", "person3", "person4", "person5", "person6", "person7", "person8", "person9"]
concat()method is used to combine two or more arrays. Instead of changing an existing array, this method returns a new array.
For detailed syntax, please refer to: concat
Height sort
Now we define a set of data in the form of height, as follows:
var aHeight = [‘170‘, ‘165‘, ‘178‘, ‘183‘, ‘168‘, ‘175‘, ‘173‘];
Reverse
We can use reverse() the method directly to implement the reverse order, as follows:
aHeight.reverse();console.log(aHeight); // ["173", "175", "168", "183", "178", "165", "170"]
The method is very simple, there is no parameter, is to change the order of the array, the first element will be the last one, the last one will become the first one. It is seldom used in general.
Sort
There are reverse() sort() more places to use the method than it is. Let's start with a short to high order, as follows:
aHeight.sort();console.log(aHeight); // ["165", "168", "170", "173", "175", "178", "183"]
sort()The default sort of method is ascending, as the above code is visible. But we can also pass in a function that specifies how it should be sorted, such as now let it be in descending order:
aHeight.sort(function(a, b){ return b - a;});console.log(aHeight); // ["183", "178", "175", "173", "170", "168", "165"]
For detailed syntax please refer to: sort
Random sort
In addition to the normal ascending descending, in fact, we often use random sorting, such as our red envelopes, chess game shuffle is a random sort of application.
When using random sorting, we have to use a random function Math.random() .
The function returns a floating-point number whose numbers are in the range [0,1].
So we can use the randomly generated floating-point number to compare with the 0.5 size, so that the result may be greater than or less than 0, and finally get our random sort.
// 第一次运行aHeight.sort(function(){ return 0.5 - Math.random();});console.log(aHeight); // ["183", "168", "175", "173", "170", "165", "178"]// 第二次运行aHeight.sort(function(){ return 0.5 - Math.random();});console.log(aHeight); // ["170", "183", "175", "168", "173", "165", "178"]
Because it is random, so each run will be different, we can run several times to try.
Conditional filtering Test
Now we define two sets of data in the form of skin color and age, as follows (yellow means yellow, white, black):
var aColor = [‘yellow‘, ‘black‘, ‘white‘, ‘white‘, ‘yellow‘, ‘yellow‘];var aAge = [19, 30, 25, 37, 18, 35];
Test whether the condition is met every
every()The method is used to test whether all the data for an array has passed the test of the specified function, if passed back true , otherwise false .
The analogy is to determine whether all people are older than 20 years old, as follows:
var ageTest = aAge.every(function(item, index){ return item > 20;})console.log(ageTest); // false
Every requires that each of the data in the array satisfy the condition is returned true , otherwise it is false .
For detailed syntax, please refer to: every
Some
Corresponding every() method, there is also a method that indicates that the some() array is returned if any one of the data satisfies the condition ture , if one of the data is not satisfied false .
The analogy is to determine whether anyone is older than 32 years old, as follows:
var ageTest2 = aAge.some(function(item, index){ return item > 32;})console.log(ageTest2); // true
For detailed syntax, please refer to: some
Includes
includes()method is used to determine whether the current array contains a specified value, and if so, returns True, otherwise false.
Metaphorically judging whether there are 35-year-olds, as follows:
var ageTest3 = aAge.includes(35);var ageTest4 = aAge.includes(28);console.log(ageTest3); // trueconsole.log(ageTest4); // false
Conditional filtering Filter
I would like to choose all the yellow skin, as follows:
var aYellow = aColor.filter(function(item, index) { return item === ‘yellow‘;})console.log(aYellow); // ["yellow", "yellow", "yellow"]
The method returns all arrays that satisfy the condition data.
For detailed syntax please refer to: Filter
Let everybody do something, foreach.
forEach()Method executes the supplied function once for each element of the array, and the method does not return a value.
Metaphor for the holidays to everyone to the boss to lead a red envelope, as follows:
var aPerson = [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]aPerson.forEach(function(item, index) { console.log(item + ‘领取了 200 元红包‘)})
For detailed syntax, refer to: ForEach
Map
map()Method creates a new array with the result that each element in the array invokes a provided function.
Each person's salary is increased by 5000 yuan, as follows:
// 先构造一份工资数据var aSalary = [8000, 7000, 1500, 9000, 22000];var aNewSalary = aSalary.map(function(item, index) { return item + 5000;})console.log(aNewSalary); // [13000, 12000, 6500, 14000, 27000]
For detailed syntax please refer to: Map
Other
In addition to the above mentioned methods, there are some common methods, such as, and indexOf join so on, here is no longer one by one explanation, specific reference: array | MDN
In short, the array of methods must be very familiar with, if you do not remember, it must also know that there is such a thing, later know how to consult, because usually do business when processing data need these various methods.
Common methods for arrays