Array operations are commonly used in JS
How to change the original array:
push: add elements to the end of the array and return the new length
pop: delete last and returns the removed element
unshift: adds an element to the beginning of the array and returns the new length
shift: shifts the first Delete elements and return deleted elements. If empty, it is undefined
reverse: reverse the order of the array
sort: sort the array
splice: delete, add, replace array elements, return the deleted array, if there is no deletion, it will not return
will not change the original array Operation method:
concat: concatenate multiple arrays and return a new array
join: separate all elements in the array with parameters Put a character
slice: Return the selected element
map(es6): Array mapping is new Array
filter(es6): Array filtering, returns all new arrays generated after judging by the method (when judged to be true)
forEach: Array traversal, no return value
every(es6): For each item in the array Run the given function, return true if each item is true, otherwise return false
some(es6): Run the given element on the array Function, if one of the items is true, it returns true. At this time, the remaining elements will not be tested again. If all of them are false, it returns false
find(es6) : Find the first element in the array that meets the conditions of the test method (function), and return the element
reduce(es6): The method receives a The function acts as an accumulator, and each value in the array (from left to right) starts to decrease, eventually counting to one value.
indexOf: The method returns the first index where a given element can be found in the array, or -1 if it does not exist.
includes(es7): The method is used to determine whether an array contains a specified value. Depending on the situation, it will be returned if it contains true, otherwise false is returned.
Use
// 连接数组 //concat方法 var array1 = ['a', 'b', 'c']; var array2 = ['d', 'e', 'f']; array1.concat(array2); // ["a", "b", "c", "d", "e", "f"] // 展开运算符方法 [...array1, ...array2] // ["a", "b", "c", "d", "e", "f"] //循环 var arr = ['a', 'b', 'c']; arr.forEach(function(element, index) { console.log(element + ',' + index); }); // a , 0 // b , 1 // c , 2 // 箭头函数写法 arr.forEach((element,index) => console.log(element,index)); //循环映射(map) var numbers = [1, 5, 10, 15]; let doubles = numbers.map((item, index) => item * 2); // [2, 10, 20, 30] // 数组是否元素包含(includes) let a = [1, 2, 3]; a.includes(2); // true a.includes(4); // false //查找元素(find) //查找数组中大于等于15的元素,并且返回第一个元素 var ret = [12, 5, 8, 130, 44].find(function(element) { return element >= 15; // 方法需要有返回值,判断得出true或者false,返回为true的元素 } ); // 130 // 过滤数组(filter) // 过滤数组中大于等于10的元素并且返回新数组 var filtered = [12, 5, 8, 130, 44].filter(function(value) { return value >= 10; // 方法需要有返回值,判断得出true或者false,返回为true的元素 } ); // [12, 130, 44] // 循环判断(every) 为每个元素都执行 var passed = [12, 5, 8, 130, 44].every(function (element, index, array) { return (element >= 10); }); // false // 循环判断(some) 遇到返回值为true的就停止执行 var passed = [12, 5, 8, 130, 44].some(function (element, index, array) { return (element >= 10); }); // true // 数组截取(slice) 不改变原数组 var animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; animals.slice(2) // ["camel", "duck", "elephant"] 返回数组从下标2开始直到结尾的一个新数组 animals.slice(2, 4) //["camel", "duck"] 返回数组从下标2到4之间到一个新数组 // 数组减接(splice) 改变原数组 var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; myFish.splice(2, 0, 'drum'); // ["angel", "clown", "drum", "mandarin", "sturgeon"] 0为删减个数,在索引为2的位置不删减并且插入'drum' myFish.splice(2, 1); // ["angel", "clown", "mandarin", "sturgeon"] 从索引为2的位置删除1项(也就是'drum'这一项) // 使用 lastIndexOf var array = [2, 5, 9, 2]; var index = array.lastIndexOf(2); // index === 3 var index = array.lastIndexOf(8); // index === -1 // 数组转字符串(join) let a = ['Wind', 'Rain', 'Fire']; a.join() //默认为逗号分隔 // 'Wind,Rain,Fire' a.join("-") // 用 - 分隔 // 'Wind-Rain-Fire' // es6 数组去重 let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; let set = new Set(array); let newarr = Array.from(set); // newarr === [1, 2, 3, 4, 5]
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to set cookies in the front-end
How to prevent event propagation in the front-end
The above is the detailed content of Array operations are commonly used in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.
