Data structure with algorithmic JavaScript reading notes

Source: Internet
Author: User
Tags array sort shallow copy

because of their operation in the array is weak, and then through the expert guidance, need to study this book, originally wanted this title is more advanced, this bad play. But looking at the things that suddenly feel like talking is more basic. But a lot of things, usually still did not notice, so write reading notes and gentlemen mutual encouragement!

The second one

2.2.1 Creating an array

How to create an array? That's more efficient? What is the reason?

The first mode of
var nums = [];
var numbers = [1,2,3,4,5];
Console.log (numbers.length)//5



Second mode

var nums = new array[];

New Array (10);
Console.log (numbers.length)//10


The first type of efficiency is more efficient.

Determine if an object is an array: You can call Array.isarray ();
The third mode of
See 2.2.3


2.2.2 Read-write array

var numbers = [1,2,3,4,5];var sum=0;if (Array.isarray (numbers)) {   //perform Array tasks on numbers    sum=numbers[0 ]+NUMBERS[1]+NUMBERS[2]+NUMBERS[3]+NUMBERS[4];    Console.log (sum);//15}

Of course, a better summation scheme is to use a for loop, an array of intrinsic properties
Numbers.length

2.2.3 generating an array from a string

var sentence = "The quick brown fox jumped over the lazy dog"; var words = Sentence.split ("");//Based on Spaces group for (var i = 0; i < words.length; ++i) {   print ("word" + i + ":" + Words[i]);}
Results see

2.2.3 integral operation of an array

Shallow copy: Var nums=[1,2,3];var samenums=nums; Nums[0]=0;console.log (Samenums[0]); 0 here is 0 instead of copying it over time 1.

Deep copy
for (Var i=0;i<nums.length;i++) {    samenums[i]=nums[i];}//This time we again:
Nums[0]=0;console.log (Samenums[0]) //1, this is still 1.

See the code above:http://www.cnblogs.com/zqzjs/p/5410138.html

2.3.1 Finding elements

  

IndexOf ();
var names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"];//putstr ("Enter a name to search for:"); var name = "Clayto n "; var position = Names.indexof (name), if (position >= 0) {   console.log ("Found" + name + "at position" + position);} else {   Console.log (name + "not found in array.");} Found Clayton at position 3

2016-04-25

a string representation of the 2.3.1 array

There are two ways to convert an array to a string: Join () and toString (). Both methods return a string containing all elements of the array, separated by commas. Here are some examples: var names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"];var namestr = Names.join ();p rint (names TR); David,cynthia,raymond,clayton,mike,jennifernamestr = names.tostring ();p rint (NAMESTR); David,cynthia,raymond,clayton,mike,jennifer in fact, when using the print () function directly on an array, the system automatically calls the ToString () method of that array: print (names); David,cynthia,raymond,clayton,mike,jennifer

2.3.3 creating a new array from an existing array

The concat () and Splice () methods allow you to create a new array from an existing array. The Concat method can combine multiple arrays to create a new array, and the splice () method intercepts a subset of an array to create a new array. Let's first look at how the Concat () method works. The initiator of the method is an array, and the argument is another array. As an array of parameters, all of the elements are connected to the array that calls the Concat () method. The following program shows how the Concat () method works: var cisdept = ["Mike", "Clayton", "Terrill", "Danny", "Jennifer"];var dmpdept = ["Raymond", "Cyn Thia "," Bryan "];var itdiv = Cis.concat (DMP);p rint (itdiv), Itdiv = Dmp.concat (cisdept);p rint (itdiv), Output: Mike,clayton, Terrill,danny,jennifer,raymond,cynthia,bryanraymond,cynthia,bryan,mike,clayton,terrill,danny,jennifer First Line first output The elements in the CIS array, and the second line first outputs the elements in the DMP array.

2.4 Variable functions

2.4.1 adding elements to an array

/** * Please use the browser's console direct copy to run the staging test * @type {number[]} *///test1var nums = [1,2,3,4,5];console.log (nums); 1,2,3,4,5nums.push (6); Console.log (nums); the//test2//unshift () method can add elements at the beginning of the array, and the following code shows the use of the method: var nums = [2,3,4,5]; Console.log (Nums); 2,3,4,5var newnum = 1;nums.unshift (newnum); Console.log (nums); 1,2,3,4,5nums = [3,4,5];nums.unshift (newnum,1,2); Console.log (nums); 1,2,3,4,5

2.4.2 removing elements from an array

Use the Pop () method to delete an element at the end of the array: var nums = [1,2,3,4,5,9];nums.pop ();p rint (nums); 1,2,3,4,5 If there is no mutable function, removing the first element from the array requires that the subsequent elements move one position ahead of each other, as inefficient as adding an element at the beginning of the array: var nums = [9,1,2,3,4,5];p rint (nums); for (var i = 0 ; i < nums.length; ++i) {Nums[i] = nums[i+1];} Print (nums); 1,2,3,4,5, in addition to moving the subsequent elements forward one bit, there is an extra element. When you print an array of elements, you will find the last comma. The shift () method deletes the first element of the array, and the following code shows the use of the method: var nums = [9,1,2,3,4,5];nums.shift ();p rint (nums); 1,2,3,4,5 this time the extra comma at the end of the array disappears. Both the pop () and Shift () methods return the deleted element as the return value of the method, so you can use a variable to save the removed element: var nums = [6,1,2,3,4,5];var first = Nums.shift (); First gets the value 9nums.push (first);p rint (nums); 1,2,3,4,5,6

2.4.4 Array Sort

The remaining two mutable methods are sorted by array. The first method is reverse (), which flips the order of the elements in the array. The following example shows how to use this method: var nums = [1,2,3,4,5];nums.reverse ();p rint (nums); 5,4,3,2,1 sorting An array is a requirement that is often encountered, and if the element is a string type, the variable method sort () of the arrays works very well: var names = ["David", "Mike", "Cynthia", "Clayton", " Bryan "," Raymond "];names.sort ();p rint (names); Bryan,clayton,cynthia,david,mike,raymond
But if the array element is a numeric type, the sort () method does not satisfy the ordering result: var nums = [3,1,2,100,4,200];nums.sort ();p rint (nums); The 1,100,2,200,3,4sort () method sorts the elements in a dictionary order, so it assumes that the elements are string types, in the previous example, even if the element is a numeric type, it is considered a string type. In order for the sort () method to sort the elements of a numeric type, you can pass in a size comparison function when the method is called, and the sort () method will compare the size of the two elements in the array according to the function to determine the order of the entire array. For numeric types, the function can be a simple subtraction operation, subtracting another number from one number. If the result is negative, then minuend is less than meiosis, and if the result is 0, then the minuend is equal to the meiosis, and if the result is positive, then the minuend is greater than the meiosis. After figuring this out, pass in a size comparison function and take a look at the previous example: function compare (NUM1, num2) {return num1-num2;} var nums = [3,1,2,100,4,200];nums.sort (Compare);p rint (nums); The 1,2,3,4,100,200sort () function uses the Compare () function to sort the array by numeric size, rather than in dictionary order.

 

Data structure with algorithmic JavaScript reading notes

Related Article

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.