The application data type in JS

Source: Internet
Author: User
Tags rounds

Detailed interpretation of the array data type

An array is an object data type, but a fine classification in the object data type.
Console.log (typeof [1, 2, 3]); --"Object"

There are two ways of defining an array
Literal way
var ary = [45, 56, 67];
Instance creation
var ary = new Array ();

Console.dir (ary);
Arrays are also composed of property names and attribute values, except that we do not have to write property names, the default property name is a number, which represents the value of the current array, and the index
0 represents the first value ary[0]
ary[index] Gets the corresponding value in the array
The value stored by the Ary.length is the number of the current array

In general, we use a for loop to iterate through each item in the array, to do the related operations.
for (var i = 0; i < ary.length; i++) {
var cur = ary[i];//Each time the loop gets the item in the current array
}

var ary = [12, 23];
Add third--add one to the end
Ary[2] = 34;
The index of the last item to add is Ary.length-1, and we add an entry after it, and the index is ary.length
Ary[ary.length] = 34;

Delete Item fourth--delete array last item
Delete Ary[3];
Delete Ary[ary.length-1];
The length property in the array is a value that can be modified
Ary.length = ary.length-1; Or
Ary.length-= 1;
Console.log (ary);


An array of commonly used methods (remember the four dimensions of these methods):
1, the role of the method
2, the parameters of the method
3, the return value of the method
4. Whether the original array has changed

var ary = [12, 23, 56, 67, 78, 89, 91, 13];

An array of additions, deletions, and modifications
1. Push () Adds a new element parameter to the end of the array: the content to be added (can be multiple values) return value: The length value of the array is changed after adding content
var res = Ary.push (78, 100);
Console.log (RES);
Console.log (ary);

2, pop () delete the last element of the array return value: The deleted element changes the original array
var res = Ary.pop ();
Console.log (RES);
Console.log (ary);

3, Shift () delete the first element of the array return value: The deleted element changes the original array
var res = Ary.shift ();
Console.log (RES);
Console.log (ary);

4, Unshift () adds a new element parameter to the beginning of the array: the content to be added (can be multiple values) return value: The length value of the array after adding content changed
var res = ary.unshift (100, 200);
Console.log (RES);
Console.log (ary);

5. Splice (n,m) deletes the M element return value starting from index N: Returns the deleted content as a new array, and the original array is changed.
Splice (n) Delete from index n to end of array--splice (0) Delete all, empty array--splice () None
var res = Ary.splice (2, 4);
Console.log (RES);
Console.log (ary);

Splice (n,m,x) starts from index n, deletes m elements, and uses X to fill the position with the return value: The deleted content is returned as a new array, and the original array is changed.
var res = Ary.splice (2, 2, "Everest Training");
Console.log (RES);
Console.log (ary);

Splice (n,0,x) starting from index N, one is not deleted, adding X to index n before the original array changed
var res = Ary.splice (3, 0, "Everest Training");
Console.log (RES);
Console.log (ary);


Second, the query of the array
var ary = [12, 23, 56, 67, 78, 89, 91, 13];
1, Slice (n,m) starting at index N, find the index m (without the M), the found content as a new array returned, the original array unchanged
Slice (n) finds the end of the array starting at index N and slice (0) copies a copy of an array identical to the original (clone of the array) <--> slice ()
var res = Ary.slice ();
Console.log (RES);
Console.log (ary);

2, concat () to implement an array of clones of the original array is not changed
var res = Ary.concat ();
Console.log (RES);
Console.log (ary);
Concat () is not in itself to achieve the cloning of the array, but to implement the concatenation of the two arrays
var a = [1, 2, 3];
var B = [2, 3, 4];
var res = A.concat (b);//a arrays are spliced with B arrays, and eventually merged into an array
Console.log (RES);

Three, the array is converted to string
1. Join
2. toString

Some common, but incompatible, methods
1. IndexOf is not compatible--lastIndexOf
2. ForEach is not compatible
3, map is not compatible

Iv. sequence of operation arrays
var ary = [12, 23, 56, 7, 78, 89, 91, 13];
1. Reverse () Arranges the array upside down and changes the original array
var res = Ary.reverse ();
Console.log (RES);
Console.log (ary);

2, sort () to implement the array to sort the original array changes but only 10 of the numbers are processed by default, and more than 10 are sorted by the first letter
Passing a parameter can implement advanced sort parameters is a function, there are two parameter A,b,return-A-B is ascending, return b-a is descending
Ary.sort (function (A, b) {
return b-a;
});
Console.log (ary);




The idea of bubble sort in array


var ary = [12, 8, 14, 9, 1];
Ary.sort (function (A, b) {
Console.log (A, b);
});
Console.log (ary);
The function inside is executed 4 times.
12 8
8 14
14 9
9 1
A is the current item for each time, and B is the latter one. Returns the difference between the current item and the next item--greater than 0 is less than or equal to 0

Algorithm for bubbling Sorting
Thought: The current item and the next item are compared, if the current want to be greater than the next item, both Exchange position
var ary = [12, 8, 14, 9, 1]; ---> [1,8,9,12,14]
First round comparison:
[8,12,14,9,1]
[8,12,14,9,1]
[8,12,9,14,1]
[8,12,9,1,14]
After the first 22 comparison is complete, we do not reach our target value, but put the current maximum value of 14 in the array at the end of the array position

Second round comparison:
[8,12,9,1,14]
[8,9,12,1,14]
[8,9,1,12,14]

Third round comparison:
[8,9,1,12,14]
[8,1,9,12,14]

Fourth round comparison:
[1,8,9,12,14]

After the completion of each round, the current largest value is placed at the end of the array position, then an array of 5 items, I only need to put the maximum value of four at the end of the implementation of the sorting;---> I compare the ary.length-1 wheel

The number of comparisons per round is also regular:
The first round is 4 times--no more than your own, so I compare up to 4 times, and before this round compares, there is no maximum number of back faces
The second Round 3 times the end of the array put a maximum value---> Not with himself, up to 4 times, and not to the end of that one, only 3 times left
The third Round 2 times the end of the array put two maximum values---> not with their own, up to 4 times, and the end of the two than, only 2 times left
......

I is the number of rounds I cycle ary.length-1 times can be

I=0 First round comparison 4 times 5-1 no more than yourself-0 no maximum value at the end
I=1 second round comparison 3 times 5-1 don't have a maximum value with yourself than-1 at the end
i=2 third round comparison 2 times 5-1 no more than oneself-2 end has two maximum value

J represents the number of times of each round J most cycles ary.length-1-i times

ARY[J] Current value ary[j+1] is the value of the latter item
If the value of the current item is greater than the value of the next item, both exchanges the position


var a=12;
var b=13;
A and B exchange positions, how the Exchange
var c=null;
C=a;
A=b;
B=c;




Bubble sort

function sortary (ary) {
    for (var i = 0; i < ary.length-1; i++) {//i is the number of rounds to compare
for (var j = 0; J < ary.length-1-I; j + +) {//j is the number of times each round is compared
if (Ary[j] > ary[j + 1]) {//If the current value is greater than the value of the next item, swap position
var temp = null;
temp = Ary[j];
ARY[J] = ary[j + 1];
Ary[j + 1] = temp;
}
}
}
return ary;
}
var ary = [12, 23, 45, 25, 12, 15, 17, 18, 1, 2, 5, 4, 8, 19];
var res = sortary (ary);
Console.log (RES);


Array de-weight


 var ary = [1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1 , 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3,  2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1 , 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4, 2, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 2, 5, 4; 
var obj = {};
for (var i = 0; i < ary.length; i++) {
var cur = ary[i];
if (obj[cur] = = cur) {
//ary.splice (i, 1);
Ary[i] = ary[ary.length-1];
Ary.length-= 1;
i--;
} else {
Obj[cur] = cur;
}
}
obj = null;
Console.log (ary);


Function data type

function is one of the reference data types, which is a functional data type, and we call functions a function, or a method, or a function

function is made up of two parts: definition (pre-processing the function that needs to be implemented), execution (execution of this method when needed, and can be executed multiple times as needed)


Develop a method of appointment (plan)
function Dating () {
1, take money 10000
2, the development of the route plan to climb the Great Wall--the Ocean pavilion--Happy Valley--the National Grand Theatre--South beauty dinner--Home
}
Execution plan
Dating ();
Dating ();
Do not know when to execute sum, will pass a few numbers, we no matter how it passes a few, there are a few I deal with a few
Console.dir (arguments); Arguments is a collection of stored pass-through parameter values that each function naturally brings, indexed by numbers, and indexed starting at 0; The Length property represents the number of values passed in the parameter; we call arguments an array of classes (object data type)
Arguments[0] The value of the first parameter arguments[1] The value of the second parameter arguments[n] The value of the n+1 parameter
Arguments.length How many parameters are there?
function sum () {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
I=0 take the first parameter value arguments[0]
I=1 take the second parameter value argumnets[1]
Arguments[i] is every time the cycle, the corresponding parameter values are taken out
var cur = number (arguments[i]);
if (!isnan (cur)) {
Total + = cur;
}
}
return total;
}
var total = SUM (1, 2, 3, 4, 5, 6, 7, 8, 9);
Console.log (total);


Date type


The application data type in JS

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.