Home > Web Front-end > JS Tutorial > An in-depth exploration of Javascript array methods_Basic knowledge

An in-depth exploration of Javascript array methods_Basic knowledge

WBOY
Release: 2016-05-16 16:21:33
Original
1006 people have browsed it

In JavaScript, arrays can be created using the Array constructor, or quickly created using [], which is also the preferred method. Array is the prototype inherited from Object, and it has no special return value for typeof, it only returns 'object'.

1. Array.prototype.slice method
The array's slice method is typically used to extract a slice from an array. However, it also has the ability to convert "array-like" objects (such as arguments and HTMLCollection) into real arrays.

Copy code The code is as follows:

var nodesArr = Array.prototype.slice.call(document.forms);
var argsArr = Array.prototype.slice.call(arguments);

I’m just curious why the array’s slice method has such a capability, and how is it implemented in the javascript engine? Does the sibling method of slice have such ability?

With curiosity, download Google’s V8 javascript engine source code locally. The download address of V8 source code: https://github.com/v8/v8.

Look for "Array.prototype.slice" in v8-master/src/array.js:

Copy code The code is as follows:

function ArraySlice(start, end) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.slice");
...
var result = []; // This sentence is the key
if (end_i < start_i) return result;
if (UseSparseVariant(array, len, IS_ARRAY(array), end_i - start_i)) {
...
SparseSlice(array, start_i, end_i - start_i, len, result);
} else {
SimpleSlice(array, start_i, end_i - start_i, len, result);
}
...
Return result;

Then I guessed that the SimpleSlice method should be used to call the "class array", and then searched for "SimpleSlice" in the source code, and found that the SimpleSlice method was also called in the Array.prototype.splice source code, and the result variable was also initialized to an empty array. However, if you want to use the splice method to convert the "array-like" into a real array, you must pass in the starting position parameter as 0, that is:

Copy code The code is as follows:

var nodesArr = Array.prototype.splice.call(document.forms, 0);

Because its implementation principle is to form a new array from the deleted array items. Interested children can take a look at the source code implementation of Array.prototype.splice.
In addition, slice can also clone an array:

Copy code The code is as follows:

var arr = [1, 2, 3];
var cloneArr = arr.slice(); // cloneArr: [1, 2, 3]

2. Array.prototype.push method
Arrays can be merged using the push method:

Copy code The code is as follows:

var arr1 = [1, 'str', {name: 'lang'}];
var arr2 = [2, 'ing'];
Array.prototype.push.apply(arr1, arr2);
// Return result: [1, "str", {name: 'lang'}, 2, "ing"]

3. Array.prototype.sort method
First enter the code:

Copy code The code is as follows:

var arr = ['1', '2', '10', '12'];
arr.sort();
// Return results: ["1", "10", "12", "2"]

The above results are usually not what we want, so how to sort by numerical value:

Copy code The code is as follows:

arr.sort(function(a, b) {
return a - b;
})
// Return results: ["1", "2", "10", "12"]

With the sorting comparator function, you can customize many comparators to achieve personalized sorting.

4. length attribute
The length attribute of the array is not read-only, which means it can also be written. For example, use the length attribute to truncate the array:

Copy code The code is as follows:

var arr = [1, 2, 3, 4];
arr.length = 2;
// arr: [1, 2]
arr.length = 0;
// arr: []

At the same time, if the length attribute is made larger, the length value of the array will increase, and undefined will be used as the new element to fill.

Copy code The code is as follows:

var arr = [];
arr.length = 3;
// arr: [undefined, undefined, undefined]

Okay, that’s all for today. It’s already early in the morning. I’ll append here if I find any new discoveries in the future.
Before, I didn’t have the habit of blogging. I was only used to putting my usual summaries in Youdao Cloud Notes. I didn’t expect that it takes a lot of thought to write out my opinions, because I have to consider how to express them so that others can better understand.

If there is any incorrect expression or misunderstanding, please help me correct it.

Also attached are some commonly used javascript array methods

concat() concatenates two or more arrays and returns the result.
join() puts all elements of the array into a string. Elements are separated by the specified delimiter.
pop() removes and returns the last element of the array
push() adds one or more elements to the end of the array and returns the new length.
reverse() reverses the order of elements in an array.
shift() deletes and returns the first element of the array
slice() returns selected elements from an existing array
sort() sorts the elements of the array
splice() removes elements and adds new elements to the array.
toSource() returns the source code of the object
toString() converts the array to a string and returns the result.
toLocaleString() converts the array to a local array and returns the result.
unshift() adds one or more elements to the beginning of the array and returns the new length.
valueOf() returns the original value of the array object

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template