JavaScript array Learning (ii)

Source: Internet
Author: User
Tags javascript array

First of all, thanks to dash , I don't have to look around for documents anymore.

Thanks again for the eclipse, let me see the world's beauty

All right, array, what's the fun way? Splice's fun.

Splice can be removed from an array to add modification actions

var myfish = [' Angel ',  ' clown ',  ' Mandarin ',  ' surgeon '];//  insert  //  Locate the following table for 2 locations   Delete 0 elements   insert   ' drum ' var removed = myfish.splice (2, 0,  ' drum ');//  myfish is [' Angel ',  ' clown ',  ' drum ",  ' Mandarin ',  ' surgeon ']// removed  is [], no elements removed//  deletes an element in the position of array 3 Removed = myfish.splice (3,  1);// myfish is [' Angel ',  ' clown ',  ' drum ',  ' Surgeon ']// removed  is [' Mandarin ']//  find the following table for 2 location   Delete 1 elements   insert   ' drum '  //  actually performing a replace operation removed =  myfish.splice (2, 1,  ' trumpet ');// myfish is [' Angel ',  ' clown ',  ' Trumpet ',  ' surgeon ']// removed is [' drum ']//  in position 0 Delete 2   add 3 removed =  Myfish.splice (0, 2,  ' parrot ',  ' anemone ',  ' Blue ');// myfish is [' Parrot ',   ' anemone ',  ' blUE ',  ' trumpet ',  ' surgeon ']// removed is [' Angel ',  ' clown ']//  Remove all subsequent elements from a location removed = myfish.splice (3, number.max_value);// myfish is [' Parrot ' ,  ' anemone ',  ' Blue ']// removed is [' trumpet ',  ' surgeon ']

Sort method

The sort method of array is not accurate at all

var fruit = [' apples ', ' bananas ', ' cherries '];fruit.sort (); [' cherries ', ' apples ', ' bananas '];var scores = [1, 2, 10, 21]; Scores.sort (); [1, ten, 2, 21]var things = [' word ', ' word ', ' 1 word ', ' 2 Words '];things.sort (); [' 1 word ', ' 2 Words ', ' word ', ' word ']//in Unicode, numbers come before upper case letters, which come before lower CAs E Letters.

It's actually pretty accurate, it's sorted by Unicode code.

So you can write your own rules.

function Compare (A, B) {if (a < b) {return-1;  } if (a > B) {return 1; }//A must is equal to B return 0;}

such as digital sorting

var numbers = [ -4,-2, 5, 1, 3];console.log (Numbers.sort ());

It's not accurate.

var numbers = [ -4,-2, 5, 1, 3];numbers.sort (function compare (A, B) {if (a < b) {return-1;  } if (a > B) {return 1; }//A must is equal to B return 0;}); Console.log (numbers);

Resolving non-ASCII string ordering

var items = [' réservé ', ' premier ', ' cliché ', ' communiqué ', ' Café ', ' Adieu '];items.sort (function (A, b) {return A.localec Ompare (b);});

Concat extended Array There's nothing to say.

Join turns the array into a string

Slice copy the specified position to form a new array

var fruits = [' Banana ', ' orange ', ' Lemon ', ' Apple ', ' Mango '];var citrus = fruits.slice (1, 3);//citrus contains [' Orange ', ' Lemon ']

Questions about new array variables formed after replication

Using slice, create newcar from mycar.var myhonda = { color:   ' Red ', wheels: 4, engine: { cylinders: 4, size: 2.2 }  };var mycar = [myhonda, 2,  ' cherry condition ',  ' purchased 1997 '];var  newcar = mycar.slice (0,  3); Console.log (MyCar); Console.log (Newcar);// change  the color of myHonda.myHonda.color =  ' Purple '; Console.log (' the new  color of my honda is  '  + myhonda.color);// display the color  of myhonda referenced from both arrays.console.log (' myCar[0].color =  '  + mycar[0].color '); Console.log (' newcar[0].color =  '  + newcar[0].color); var  mycar = [];mycar = [{color: ' Red ',  wheels:4, engine:{cylinders:4, size :2.2}}, 2,  ' CherrY condition ',  ' purchased 1997 '];var newcar = [{color: ' Red ', wheels:4,  engine:{cylinders:4, size:2.2}}, 2];mycar[0].color =  ' Purple '; Console.log (MyCar); Console.log (Newcar);

From the example above, it's not entirely different.

JavaScript array Learning (ii)

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.