JS basics-array summary, js array Summary

Source: Internet
Author: User

JS basics-array summary, js array Summary

Arrays in JS are considered as an object. How can we suddenly feel that any concept that can be independent in JS can be interpreted as an object? A little skeptical. Continue. First, we will summarize how arrays are used in JS.

I. Create

The array is basically the same in JS as in Object. It can be declared by the new keyword or created by using the literal method. In JS, only one-dimensional arrays can be created.

1. new

Var box = new Array (); // The new Keyword can be omitted. The Array can be initialized internally or the number of elements can be declared. This is the same for other languages.

2. Literal

Var box = []; // initialization can also be performed

3, Note:1. By default, each element in the array is separated by commas (,). The Join () method can be used to separate the array elements with different delimiters, for example, Join ('| ')

2. array elements in JS can be of any type or even an array. Create a complex array:

Var box = [{// The first element is the name: 'wyy', age: 22}, 'computer programming ', [1, 2, 45, 6, new Object ()], // create an Array containing an Object 25 + 34, new Array (6, 7, 8) // here is an array]

Ii. Element assignment and call 1. Assignment

When assigning values to array elements, you can directly initialize them to assign values to each element, but this method is not flexible enough. If we want to dynamically add elements to an array, in this case, you need to assign values to the elements in the array externally. You can use the name of each element in the array to assign values to it, and you can use the element subscript to assign values.

Var box = []; box. name = 'wyy'; box. age = 22; alert (box ['name']); // It is not supported if alert (box) directly pops up the array box. Blank
Or use the array subscript.
Var box = []; box [0] = 'wyy'; box [1] = 22; alert (box); // you can use subscript to pop up the element content in the box.
Given the differences between the two methods above, we usually use index subscript to assign values when assigning values. According to the assignment method, the principle of array is the same when it is called. It is called by the element name and index subscript. At the same time, the length attribute can be used to obtain the number of array elements and the value of the last element in the array.

Iii. Object and Operation Method object Method

1. Stack

Push: add an element syntax at the end of the array: box. push ('20140901') Note that if it is alert (box. push ('20160901'), the length of the latest array will be returned.

Pop: remove the element at the end of the array: syntax alert (box. pop () // returns the value of the removed element, which is different from push.

2. Queue

Push: The push method in the same stack

Shift: remove the first element Syntax: alert (box. shift () // print the removed Element

Unshift: add an element to the front end of the array and return the length of the new array. Box. unshift ('000000') // incompatible

3. Sorting

Reverse: returns an array in reverse order,The original array is also in reverse order, indicating that the array is referenced.

Sort: sort from small to large. before using this function, you must declare its sorting rules. It can be reversed.

Operation Method:

1. concat: append a new array based on the original array.

2. the slice () and spice () methods are widely used. The parameters and rules are not fixed. You need to differentiate them carefully when using them.

Slice is mainly used for values,But does not affect the value of the original array..

Spice is used to delete specified elements in an array. For example, Box. splice () // starts from the first element, takes three elements, and deletes these elements from the original array. It can also be used to insert elements, for example:

Box. splice (, 'wlj', 'dsf') // Insert the following element after the first element

Var box1 = box. splice (, 'Hello'); // The second parameter here is 0, so there is no location to delete, so the meaning is changed to insert an element 'hello' after the first location '.


Summary: This part of the knowledge is broken, and there is not much knowledge on the principle. We must link the learned knowledge to the net and give it a line, just like a fixed pattern, you can associate them in terms of creation and usage, review them more, and deepen your understanding through examples.



Js array Problems

[{},{},{}] Is an array.
For (var I = 0; I <topModules. length; I ++ ){
Var obj = topModules [I];

Alert (obj. title );

Alert (obj ['title']);

Obj. title = "New Title ";

}

In this way, it seems that this topModules is an attribute of an object. If so, the above reference will add that object.

Js Array Operations

Function check (arr ){
For (I = 0; I <arr. length; I ++ ){
For (j = I + 1; j <arr. length; j ++ ){
If (arr [I] = arr [j]) return true;
}
}
Return false;
}
If there are duplicates, true is returned, and false is not returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.