Array of JavaScript notes keyword (store and release & stack & by value Reference)

Source: Internet
Author: User

1. Array creation and initialization

var obj=new Array ();

var arr=[];

Can be extended to a certain length, the literal defines the array

2. Stack by value Pass reference type

An array is a reference type, not a value pass,

Stack: Automatic allocation and release of the barrel, such as function parameters, local variables advanced after the first-level cache

Heap: Usually released by the programmer, the system recycles the FIFO two cache at the end of the program

3. Associative arrays and indexed arrays

Associative array is a hash array, essentially JS bottom, all objects are associative arrays

   So the following writing can produce running results,    can run the result: 2   var keys=[1,2,3,4];   Console.log (keys["1"]);

4. Delete the repeating elements in the array: (Conventional solution &hash array solution comparison)

1. General method of thinking: Create an array, and then use the original array of elements and all the elements inside the new array to compare, if different, this element is added to the last element of the new array

            functionF (ARR)//implements the deletion of duplicate elements within an array            {             varArrc=[]; //take out the first element of Arr, put the first one in the ARRC arrayArrc[0]=arr[0]; //iterates through the ARR array, starting with 1, extracting the elements, and comparing elements in the ARRC (copy array)             for(vari=1;i<arr.length;i++)            {             for(varj=0;j<arrc.length;j++)            {            if(arrc[j]==Arr[i]) Break;//The same is the end of the search, jumping out of the loop} J==arrc.length&& (Arrc[arrc.length]=arr[i]);//unequal is assigned to the last element of the array            }            returnARRC; } console.log (f ([1,2,2,1,4,5]));
     Operation result: [1,2,4,5]

2. Using hash array to solve, using the characteristics of hash, greatly improve the efficiency, note that the return is a character type, can be converted as needed.

functionF1 (arr) {//Create a new hash array                varHash=[]; hash[arr[0]]=1;//Initialize the first element of a hash array                 for(i=1;i<arr.length;i++)                {                    if(hash[arr[i]]==undefined) hash[arr[i]= 1;//if the corresponding element of arr[i] in the hash array is undefined, the description is not duplicated, it is placed in a hash array                }                //after the loop is over, remove the hash array                varback=[];  for(Back[back.length]inchhash); returnBack ; } console.log (F1 ([1,2,2,1,4,5]));
          Operating results: ["1", "2", "4", "5"]

On the efficiency of execution, especially when the data increases, the hash array does not need to go through the array lookup, so the execution efficiency will be much higher than the conventional method.

Array of JavaScript notes keyword (store and release & stack & by value Reference)

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.