JS handles arrays in various ways
Data types in js are divided into two categories: primitive types and object types.
Primitive types include: numerical, string, boolean, null, undefined
Object types include: objects are collections of attributes, and of course there are two special objects here--functions (first-class objects in js), arrays (ordered sets of keys and values).
Adding array elements
arrayObj.push([item1 [item2 [. . . [itemN ]]]);
Add one or more new elements to the end of the array and return the new length of the array
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]);
Add one or more new elements to the array to start, the elements in the array will automatically move backwards, returning the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);
Insert one or more new elements into the specified position of the array, the elements at the inserted position will automatically move backwards, returning to ""
Deletion of array elements
arrayObj.pop();
Remove the last element and return the value of that element
arrayObj.shift();
Remove the last element and return the element value, and the elements in the array will automatically move forward.
arrayObj.splice(deletePos,deleteCount);
Delete the element of the specified number of deleteCount starting from the specified position deletePos, and returns the removed element in the array form
Intercept and merge
arrayObj.slice(start, [end]);
Return part of the array as an array, note that the elements corresponding to end are not included. If end is omitted, all elements after start will be copied.
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]);
Concatenate multiple arrays (can also be strings, or a mixture of arrays and strings) into an array, and return the connected new array
Copy of array
arrayObj.slice(0);
Return the copy array of the array, note that it is a new array, not a pointer
arrayObj.concat();
Return the copy array of the array, note that it is a new array, not a pointer
Sort array elements
arrayObj.reverse();
Reverse the element (the first one is ranked last, the last one is ranked last), and return the array address
arrayObj.sort();
Sort array elements and return array address
Insert array elements
arrayObj.splice(insertPos,0, [item1[, item2[, . . . [,itemN]]]]);
Insert the specified item element from the insertPos position, 0 means deletion of 0 elements and return an empty array
Replacement of array elements
arrayObj.splice(insertPos,replaceCount, [item1[, item2[, . . . [,itemN]]]]);
Delete replaceCount elements from insertPos location, and then add the specified item element from insertPos location, and return the deleted element in the array
The location of the array element
arrayObj.indexOf(findThing,start);
Start looking backwards from the starting point to be found (optional) start, findThing, the search criteria are congruent, if it is found, return the position of the value, if it is not found, return -1 if it is not found, it is found.
arrayObj.lastIndexOf(findThing, number)
Start looking for the item to be found from the starting point to be found (optional) start. The search criteria are congruent. If it is found, it returns the location of the value. If it is not found, it returns -1 if it is not found.
Iteration of array elements
arrayObj.every()
Run the given function on each item in the array, and if the function returns true for each item, it returns true
arrayObj.filter()
Run the given function on each item in the array. Returning the array consisting of items that return true
arrayObj.forEach()
Run the given function on each item in the array, and this method does not return a value
arrayObj.map()
Run the given function on each item in the array, and return the array composed of the result of each function call
arrayObj.some()
Run the given function on each item in the array, and if the function returns true for either item, it returns true
Combination of array elements
arrayObj.reduce(prev, cur, index, array)
Starting from the first item of the array, one by one to the end, the four parameters are the previous value, the current value, the index of the item and the array object. Any value returned by the function will be automatically passed to the next item as the first parameter as the next item.
000
arrayObj.reduceRight()
Starting from the end of the array, one by one to the first item, the four parameters are the previous value, the current value, the index of the item and the array object. Any value returned by the function will be automatically passed to the next item as the first parameter as the next item
Stringing of array elements
arrayObj.join(separator);
Returns a string that connects each element value of the array together, separated by a separator.
toLocaleString , toString , valueOf: It can be regarded as a special usage of join, not often used
toSource() returns the source code of the object
toString() converts the array into a string and returns the result
toLocaleString() converts the array into a local array and returns the result
valueOf() Returns the original value of the array object
ES5 new
Array.prototype.indexOf
The indexOf() method returns the position of the first element found in the array, and -1 if it does not exist.
Array.prototype.lastIndexOf
Array.prototype.every
Array.prototype.some
Array.prototype.forEach
forEach executes the corresponding method for each element
Array.prototype.map
After map() performs a certain operation (map) on each element of the array, a new array will be returned
map() is a very practical function when processing data returned by the server
Array.prototype.filter
The filter() method creates a new array matching the filter criteria.
Array.prototype.reduce
reduce() can implement the function of an accumulator, reducing each value of the array (from left to right) to a value
Scenario: Statistics how many unrepeatable words are in an array
Array.prototype.reduceRight
The above method about array objects in JavaScript (detailed explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.