Netease front-end micro professional, JavaScript programming basics: Array
Arrays of any language are important. As a basic object, arrays are widely used. For example, Java, you must add a List or Map. Therefore, this article mainly records the use of JS arrays and common methods. Key points are as follows:
1. Create an array
Two methods:
var stu = new Array();
var stu1 = [];
This is the same as defining an object:
var cat = new Object(); var cat1 = {};
The latter is recommended, which is concise. For example:
var score = [1, 2, 3];
The items in the array can be of different types, which can be basic or object or array:
var array = [ 163, "netease", {color: "red"}, [], true ]; console.log(array[0]); console.log(array[2].color);
Next:
2, length () function, get the length of the array
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; console.log(stu.length); // 3 stu = []; console.log(stu.length); //0
Access and modify the I-th element through stu [I.
3. indexof () function. If you can find the index that is returned,-1 is not found, and you have the courage to judge that an element is not in the array.
var tel = [101, 110, 139]; var index = tel.indexOf(101); console.log(index);
4. forEach () function
ForEach needs to accept a callback function
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; var add5 = function(item, number, array){ item.score += 5; }; stu.forEach(add5); console.log(stu[0].score);
This callback requires that the input parameters be the current item, item index, and the entire array. forEach will automatically traverse each row and send each row to the callback function for processing.
5. The reverse () function sorts the array in reverse order.
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.reverse(); console.log(stu[0].score);
6. array. sort () function, which passes in a callback, similar to java sorting.
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; var bySocre = function(a, b){ return b.score - a.score; } stu.sort(bySocre); var print_callback = function(item, number, array){ console.log(item.score); } stu.forEach(print_callback);
Note:
A. if false is returned in callback, a is placed before B. If the return value is B-a, it is from large to small, and vice versa.
B, sort directly changes the original array.
C. If callback is not passed, the unicode code is sorted in ascending order:
var names = ["yanzi", "gg", "ww"]; names.sort(); var print_callback = function(item, number, array){ console.log(item); } names.forEach(print_callback);
7,array.push()
Add multiple elements to the end of an existing array.
8,array.unshift()
Add elements at the beginning of the array.
Sample Code:
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.push({id:4, score:100}); stu.unshift({id:5, score:99}); var print_callback = function(item, number, array){ console.log(item.id); } stu.forEach(print_callback);
9. array. shift () returns the first element and deletes the first element in the original number group.
10. array. pop () returns the last element and deletes the last element from the original number group.
11. array. splice (a, B, C) needs to input three parameters, starting from position a, deleting B elements, then inserting the element C, C can be multiple
Sample Code:
var stu = [ {id:1, score : 80}, {id:2, score : 75}, {id:3, score : 90}, ]; stu.splice(1,1,{id : 5, score:100}); var print_callback = function(item, number, array){ console.log(item.id); } stu.forEach(print_callback);
Note: If the third splice parameter is not input, it will only be deleted. If the second parameter is set to 0, it is inserted and not deleted.
Summary: reverse, sort, push, unshift, shift, pop, and splice all share a common feature that changes the original array.
12. Copy array. slice (start, end) from index start to end-1 to return a copy. If the end parameter is not passed, it is truncated to the last position.
13. array. concat (a, B): connects array a and array B.
14. array. join (a) concatenates each element of array with. If nothing is passed, it is used by default and the number is separated.
15. An array. map () callback must be passed in, and return is required for the callback. By default, return is pushed to a new array.
var scores = [80, 75, 90]; var addScore = function(item, index, array){ return item + 5; } var scoresNew = scores.map(addScore); var print_callback = function(item, number, array){ console.log(item); } scoresNew.forEach(print_callback);
16,array.reduce()
Call back (preResult, item, index, array.
var scores = [80, 75, 90]; var sum = function(preresult, item, number, array){ return preresult + item; } var sum2 = scores.reduce(sum, 0); console.log(sum2);
Summary: slice, concat, join, map, and reduce do not modify the original array.