About the method of increasing
1.push
Note 1: It adds the new element to the end of the array. Note 2: It changes the array and returns the length of the new array.
var arr=[1.2.3]
var newarr = arr.push(4)
console.log(arr); / / [1, 2, 3, 4]
console.log(newarr);/ / 4
Copy the code
2.unshift
Note 1: It adds the new element to the head of the array. Note 2: It changes the array. Note 3: It returns the length of the new array
var arr=[1.2.3]
var newarr = arr.unshift(5)
console.log(arr);/ /,1,2,3 [5]
console.log(newarr);/ / 4
Copy the code
3.splice
Here comes the heavyweight. It can not only add elements, remove elements, but also replace them. Let’s talk about how to add them
Note 3: Parameter 1 is the index of the position you want to add, parameter 2 is 0, and parameter 3 is the element you want to add.
var arr=[1.2.3]
var newarr = arr.splice(0.0.1)
console.log(arr);/ /,1,2,3 [1]
console.log(newarr);/ / []
Copy the code
4.concat
This method often uses a concatenated array
Note 1: This method does not change the original array
var arr=[1.2.3.2.9]
var newarr = arr.concat(1.2)
console.log(arr);/ /,2,3,2,9 [1]
console.log(newarr);/ /,2,3,2,9,1,2 [1]
var arr2=[1.0]
var newarr2 = arr.concat(arr2)
console.log(newarr2);/ /,2,3,2,9,1,0 [1]
Copy the code
About the method of deletion
1.pop
Note 2: Change the original array note 3: Return the deleted element
var arr=[1.2.3]
var newarr = arr.pop()
console.log(arr);/ / [1, 2]
console.log(newarr);/ / 3
Copy the code
2.shift
Note 1: Delete the header element. Note 2: change the array. Note 3: return the deleted element
var arr=[1.2.3]
var newarr = arr.shift()
console.log(arr);/ / [2, 3]
console.log(newarr);1
Copy the code
3.splice
Old friends again, this time to see the use of delete
Note 1: Delete elements anywhere note 2: change groups of elements note 3: Return an array of deleted elements (you read that right, array!!) Note 4: parameter 1: delete the index of start, parameter 2: delete several elements
var arr=[1.2.3]
var newarr = arr.splice(0.1)
console.log(arr);/ / [2, 3]
console.log(newarr); [1]
Copy the code
4.slice
This is more of an interception, so I’ll put it in that category
2. It does not alter the array, and returns the truncated elements in a new array
var arr=[1.2.3.2.9]
var newarr = arr.slice(1.2)
console.log(arr);/ /,2,3,2,9 [1]
console.log(newarr);/ / [2]
Copy the code
To change the method of
1.splice
Note 1: Change the element in any position note 2: change the original array Note 3: return the new array to replace the element note 4: Parameter 1: as above, Parameter 2: delete several elements, parameter 3: fill in the element
var arr=[1.2.3]
var newarr = arr.splice(0.1.4)
console.log(arr);/ / (4, 2, 3)
console.log(newarr);/ / [1]
Copy the code
Check the operation of the
1.indexOf
Note 1: pass in the element you are looking for, return the element’s index in the array if found, return -1 if not found
var arr=[1.2.3]
var newarr = arr.indexOf(1)
console.log(newarr);/ / 0
Copy the code
2.includes
Note # 1: Pass in the element you are looking for and return true/false
var arr=[1.2.3]
var newarr = arr.includes(1)
console.log(newarr);//true
Copy the code
3.find
Note: What is passed in is a function that returns the first matching element
var arr=[1.2.3.2.9]
var newarr = arr.find((item,index) = >{
return item>2
})
console.log(newarr);/ / 3
Copy the code
Sorting method
1.sort
Note: Sort the original array
var arr=[1.2.3.2.9]
arr.sort()
console.log(arr);/ /,2,2,3,9 [1]
Copy the code
It can also be customized
var arr=[1.2.3.2.9]
//e1 indicates the former element and e2 indicates the latter element
arr.sort((e1,e2) = >{
return e1-e2
})
console.log(arr);/ /,2,2,3,9 [1]
Copy the code
2.reverse
Note: Reverse the original array
var arr=[1.2.3.2.9]
arr.reverse()
console.log(arr);
Copy the code
Conversion method
1.join
Note: Convert an array to a string without changing the original array
var arr=[1.2.3.2.9]
var newarr= arr.join(', ')
console.log(newarr);/ / 1,2,3,2,9
console.log(arr);/ /,2,3,2,9 [1]
Copy the code
An iterative approach
1.some
Note: Pass each item of the array into the function, and return true if one of the items matches the addition
var arr=[1.2.3.2.9]
var newarr= arr.some((item,index) = >{
return item>2
})
console.log(newarr);//true
Copy the code
2.every
Note: Pass each item in the array to a function, returning true only if each item meets the criteria
var arr=[1.2.3.2.9]
var newarr= arr.every((item,index) = >{
return item>2
})
console.log(newarr);//false
Copy the code
3.forEach
ForEach (function(){},obj) is simply a quick way to iterate over an array, passing each element of the array into the function first
var arr=[1.2.3.2.9]
var newarr= arr.forEach(element= > {
console.log(element);
})
console.log(newarr);//undefined
Copy the code
4.map
Note 1: the offer is a mapping function, an array of each item into the into the function Note 2: element passes through in the function instruction for all kinds of transform, generating new elements, and will return to destined to point 3: new elements will eventually return to all the elements to form a new array, the array is the map () returns the value of in the end
var arr=[1.2.3.2.9]
var newarr= arr.map(element= > {
return element*2
})
console.log(newarr);//[2, 4, 6, 4, 18]
Copy the code
5.reduce
Arr. Reduce (callback, initialValue) callback (a function called on each item in the array, which takes four functions: PreviousValue (the value returned when the callback was last called, Or initialValue) currentValue (array element currently being processed) currentIndex (subscript of array element currently being processed) array (array calling reduce()) initialValue (optional initialValue. The value passed to previousValue as the first call to the callback function)
Reduce application: 1. Do accumulation 2. Flatten array 3. Array deduplication and so on
Common array interview questions
1. Array deduplication
(1) the use of the set
var arr=[1.2.3.2.9]
var newarr = [...new Set(arr)]
console.log(newarr);/ / [1, 2, 3, 9]
Copy the code
Use indexOf (2)
var arr=[1.2.3.2.9]
function fn(arr){
var newarr=[]
arr.filter((item,index) = >{
if(newarr.indexOf(item)==-1){
newarr.push(item)
}
})
return newarr
}
console.log(fn(arr));/ / [1, 2, 3, 9]
Copy the code
(3) use includes
var arr=[1.2.3.2.9]
function fn(arr){
var newarr=[]
arr.filter((item,index) = >{
if(! newarr.includes(item)){ newarr.push(item) } })return newarr
}
console.log(fn(arr));
Copy the code
(4) using the reduce
var arr=[1.2.3.2.9]
function fn(arr){
var newarr=[]
arr.sort().reduce((pre,value,index) = >{
// Compare the value of the last element in pre to the current value
if (pre[index-1]! ==value) { newarr.push(value) }return newarr
},[])
return newarr
}
console.log(fn(arr));
Copy the code
2. Array flattening
(1) Use flat
Note that this API can only pass the depth up to 1 without passing the parameter, which represents the depth, and can pass Infinity if the depth is not known
var arr=[1.2.3.2.9[2.3[1]]]
console.log(arr.flat());//[1, 2, 3, 2, 9, 2, 3, Array(1)]
var arr=[1.2.3.2.9[2.3[1]]]
console.log(arr.flat(Infinity));//[1, 2, 3, 2, 9, 2, 3, 1]
Copy the code
(2) Use reduce
var arr=[1.2.3.2.9[2.3[1]]]
function flat(arr){
return arr.reduce((pre,value,index) = >{
return pre.concat(Array.isArray(value)? flat(value):value) },[]) }console.log(flat(arr));//[1, 2, 3, 2, 9, 2, 3, 1]
Copy the code
(3) Convert an array to a string and then to an array
var arr=[1.2.3.2.9[2.3[1]], [2.3]]
// Convert an array to a string, and then convert a string to an array
var newarr = arr.join(', ').split(', ').map((item,index) = >{
return parseInt(item)
})
console.log(newarr);//[1, 2, 3, 2, 9, 2, 3, 1, 2, 3]
Copy the code
3. Determine the number of elements in the array
For example, var arr=[‘name’,’age’,’ HHH ‘,’ HHH ‘] how many times each occurs
var arr=['name'.'age'.'hhh'.'hhh']
var newarr = arr.reduce((pre,item,index) = >{
if(! pre[item]) { pre[item]=1
}else{
pre[item]++
}
return pre
},{})
console.log(newarr);
Copy the code
4. Determine the element in the array that occurs most often
On the basis of counting the number of occurrences of each element
var arr=['name'.'age'.'hhh'.'hhh']
var newarr = arr.reduce((pre,item,index) = >{
if(! pre[item]) { pre[item]=1
}else{
pre[item]++
}
return pre
},{})
var max=0// The maximum number
var attr// What is the most common attribute
for (const key in newarr) {
/ / the key for attributes
console.log(newarr[key]);
if (newarr[key]>max) {
max=newarr[key]
attr=key
}
}
console.log(max,attr);
Copy the code
5. Count duplicate elements in the array
var arr=[1.2.6.2.9.1]
console.log(arr.sort());
var arr2=[]
arr.sort().sort((e1,e2) = >{ e1===e2? arr2.push(e2):' '
})
console.log(arr2);
Copy the code
6. In A, not in B
Const a =[1,2,3,5] const b=[1,3,5,6]
const a = [1.2.3.5]
const b=[1.3.5.6]
const newarr = a.filter((item,index) = >{
// Select a from b
return b.indexOf(item)==-1
})
console.log(newarr);
Copy the code
Comprehensive problem sets
The company has recruited 10 employees (gender, age and monthly salary vary) with the following requirements: 1). The list displays all information of all employees. 2). The annual salary of employees is displayed in descending order. Get the total monthly salary of male employees 4). Find an employee whose monthly salary is only a little over 12000 5). Find all employees with a monthly salary above 12000
const employees = [
{name: 'A'.sex: 'male'.age: 21.salary: 10000},
{name: 'B'.sex: 'woman'.age: 25.salary: 12000},
{name: 'C'.sex: 'male'.age: 24.salary: 13000},
{name: 'D'.sex: 'male'.age: 24.salary: 12500},
{name: 'E'.sex: 'woman'.age: 21.salary: 14000},
{name: 'F'.sex: 'male'.age: 24.salary: 16000},
{name: 'G'.sex: 'male'.age: 23.salary: 9000},
{name: 'H'.sex: 'woman'.age: 21.salary: 11000},
{name: 'I'.sex: 'male'.age: 23.salary: 13200},
{name: 'J'.sex: 'male'.age: 23.salary: 15000}]Copy the code
1)employees.forEach((item,index) = >{
console.log(item);
})
2)var newArr= employees.sort((e1,e2) = >{
return e2.salary-e1.salary
})
console.log(newArr);
3)var newArr= employees.reduce((pre,item) = >{
return pre = item.sex==='male'? pre+item.salary:0
},0)
console.log(newArr);
4)var newArr= employees.sort((e1,e2) = >{
return e1.salary-e2.salary
}).find((item,index) = >{
return item.salary>12000
})
console.log(newArr);
5)var newArr = employees.filter((item,index) = >{
return item.salary>12000
})
console.log(newArr);
Copy the code