1. concat() var arr1 = [1,2,3]; var arr2 = [4,5]; var arr3 = arr1.concat(arr2); console.log(arr1); //[1, 2, 3] console.log(arr3); //[1, 2, 3, 4, 5] 2. join() The var arr = [2,3,4]; console.log(arr.join()); //2,3,4 console.log(arr); //[2, 3, 4] 3. push() The var a = [2,3,4]; var b = a.push(5); console.log(a); //[2,3,4,5] console.log(b); //4 5. shift() var arr = [2,3,4]; console.log(arr.shift()); //2 console.log(arr); //[3,4] 6. unshift() The var arr = [2,3,4,5]; console.log(arr.unshift(3,6)); //6 console.log(arr); //[3, 6, 2, 3, 4, 5] Tip: This method does not need to pass parameters. If no parameters are passed, no elements will be added. 7. slice() var arr = [2,3,4,5]; console.log(arr.slice(1,3)); //[3,4] console.log(arr); //[2,3,4,5] splice() var a = [5,6,7,8]; console.log(a.splice(1,0,9)); //[] console.log(a); // [5, 9, 6, 7, 8] var b = [5,6,7,8]; console.log(b.splice(1,2,3)); //[6, 7] console.log(b); //[5, 3, 8] 9. substring() and substr()Similarities: If you only write one parameter, the functions of both are the same: both are to intercept the string fragment from the current subscript to the end of the string. substr(startIndex); substring(startIndex); var str = '123456789'; console.log(str.substr(2)); // "3456789" console.log(str.substring(2)) ;// "3456789" Differences: The second parameter console.log("123456789".substr(2,5)); // "34567" console.log("123456789".substring(2,5)) ; // "345" 10. sort Sort by var fruit = ['cherries', 'apples', 'bananas']; fruit.sort(); // ['apples', 'bananas', 'cherries'] var scores = [1, 10, 21, 2]; scores.sort(); // [1, 10, 2, 21] 11. reverse() The var arr = [2,3,4]; console.log(arr.reverse()); //[4, 3, 2] console.log(arr); //[4, 3, 2] 12. indexOf and lastIndexOf Both var a = [2, 9, 9]; a.indexOf(2); // 0 a.indexOf(7); // -1 if (a.indexOf(7) === -1) { // element doesn't exist in array } lastIndexOf var numbers = [2, 5, 9, 2]; numbers.lastIndexOf(2); // 3 numbers.lastIndexOf(7); // -1 numbers.lastIndexOf(2, 3); // 3 numbers.lastIndexOf(2, 2); // 0 numbers.lastIndexOf(2, -2); // 0 numbers.lastIndexOf(2, -1); // 3 13. every pair array function isBigEnough(element, index, array) { return element < 10; } [2, 5, 8, 3, 4].every(isBigEnough); // true 14. some function compare(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(compare); // false [12, 5, 8, 1, 4].some(compare); // true 15. Filter var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"]; var longWords = words.filter(function(word){ return word.length > 6; }); // Filtered array longWords is ["exuberant", "destruction", "present"] 16. MapRuns the given function on each item in an array, returning a new array consisting of the results of each function call. var numbers = [1, 5, 10, 15]; var doubles = numbers.map(function(x) { return x * 2; }); // doubles is now [2, 10, 20, 30] // numbers is still [1, 5, 10, 15] 17. forEach array traversalconst items = ['item1', 'item2', 'item3']; const copy = []; items.forEach(function(item){ copy.push(item) });
1. find():Pass a callback function to find the first element in the array that matches the current search criteria, return it, and terminate the search. const arr = [1, "2", 3, 3, "2"] console.log(arr.find(n => typeof n === "number")) // 1 2. findIndex():Pass a callback function to find the first element in the array that matches the current search criteria, return its index, and terminate the search. const arr = [1, "2", 3, 3, "2"] console.log(arr.findIndex(n => typeof n === "number")) // 0 3. fill():Replace the elements in the array with new elements. You can specify the replacement subscript range. arr.fill(value, start, end) 4. copyWithin():Select an index of the array and start copying the array elements from that position. The default is to start copying from 0. You can also specify a range of elements to copy. arr.copyWithin(target, start, end) const arr = [1, 2, 3, 4, 5] console.log(arr.copyWithin(3)) // [1,2,3,1,2] starts from the element with index 3 and copies the array, so 4 and 5 are replaced by 1 and 2 const arr1 = [1, 2, 3, 4, 5] console.log(arr1.copyWithin(3, 1)) // [1,2,3,2,3] starts from the element with index 3 and copies the array. The index of the first element to be copied is 1, so 4 and 5 are replaced by 2 and 3. const arr2 = [1, 2, 3, 4, 5] console.log(arr2.copyWithin(3, 1, 2)) // [1,2,3,2,5] starts from the element with index 3 and copies the array. The first element to be copied is index 1 and the end position is 2, so 4 is replaced by 2. 5. from Convert const bar = ["a", "b", "c"]; Array.from(bar); // ["a", "b", "c"] Array.from('foo'); // ["f", "o", "o"] 6. of Used to convert a set of values into an array. The main purpose of this method is to make up for the shortcomings of the array constructor Array() // [] Array(3) // [, , ,] Array(3, 11, 8) // [3, 11, 8] Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3] 7. entries() returns an iterator: returns key-value pairs//array const arr = ['a', 'b', 'c']; for(let v of arr.entries()) { console.log(v) } // [0, 'a'] [1, 'b'] [2, 'c'] //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.entries()) { console.log(v) } // ['a', 'a'] ['b', 'b'] ['c', 'c'] //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.entries()) { console.log(v) } // ['a', 'a'] ['b', 'b'] 8. values() returns an iterator: returns the value of the key-value pair//array const arr = ['a', 'b', 'c']; for(let v of arr.values()) { console.log(v) } //'a' 'b' 'c' //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.values()) { console.log(v) } // 'a' 'b' 'c' //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.values()) { console.log(v) } // 'a' 'b' 9. keys() returns an iterator: returns the key of the key-value pair//array const arr = ['a', 'b', 'c']; for(let v of arr.keys()) { console.log(v) } // 0 1 2 //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.keys()) { console.log(v) } // 'a' 'b' 'c' //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.keys()) { console.log(v) } // 'a' 'b' 10. includes Determines whether the element exists in the array. Parameters: the value to be searched, the starting position. This can replace var a = [1, 2, 3]; a.includes(2); // true a.includes(4); // false This is the end of this article about commonly used array operation methods in JavaScript. For more relevant JavaScript array operation methods, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Nginx URL rewriting mechanism principle and usage examples
>>: Mysql implementation of full-text search and keyword scoring method example
This article example shares the specific code of ...
We all know the drag-and-drop feature of HTML5, w...
Let's take a look at zabbix monitoring sqlser...
Table of contents Question: When the button is cl...
MySQL Introduction to MySQL MySQL was originally ...
1. The role of brackets 1.1 Square brackets [ ] W...
in conclusion % of width: defines the percentage ...
Table of contents Install jupyter Docker port map...
Preface: The "Getting Started with MySQL&quo...
1. Development environment vue+vant 2. Computer s...
Anyone who has worked on a large system knows tha...
Or write down the installation process yourself! ...
Floating ads are a very common form of advertisin...
It is not possible to use width and height directl...
Serialization implementation InnoDB implements se...