JS in the array of those things ~

Source: Internet
Author: User
Tags array to string

Busy today, see some of the operation of the array, long time wood has been useful, many have forgotten almost, so today spent one hours to write some demo, ready to back up the use of a convenient later ~ ~ ~

Here are some of the work, often used in the array of methods, small partners can choose their own preferences ~ ~ ~

<! DOCTYPE html>vararr= [1,2,3,4,5], arr1= [10,4,5,2,8,99]; /*a instanceof b A is not a B made out of*/Console.log (arrinstanceofArray)//true            /*Array.isarray (a) whether variable A is an array*/Console.log (Array.isarray (arr))//trueConsole.log (Array.isarray (2))//false            /*toString () array to String, with "," link; Join also achieves this effect*/Console.log (arr.tostring ())/*array. Join (' symbol ') to form a string with the array as a symbolic link*/Console.log (Arr.join ($)) Console.log (Arr.join (‘ ‘))            /*Push adds a pop at the end of the array to delete at the end of the array*/Arr.push (' Paoafter ') Console.log (arr);            Arr.pop () Console.log (arr); /*unshift Add shift at the front of the array to delete the first array*/Arr.unshift (' Paounshift ') Console.log (arr) arr.shift () Console.log (arr)/*Sorting of Arrays*/          /*Reverse Array Rollover*/Console.log (Arr.reverse ())/*sort ()???? Array sorting, small to large*/Console.log (Arr.sort ())//using callback functions to sort the array sizeArr1.sort (function(A, b) {returnAb; }) Console.log (arr1)/*operation of array elements*/           /*array 1.concat (array 2) splicing of two arrays*/Console.log (Arr.concat (arr1))//array de-weight 1:Array.prototype.arrUnique =function () {               //Sort First                This. sort (); //defines a new array to receive values from the first start of a sorted array               vararr = [ This[0]]; //Compare starting with the second value                for(vari = 1; I < This. length; i++) {                   //when comparing adjacent values, the new array receives the value and achieves the de-weight effect .                   //the first of the original array is compared to the last one in the new array                   if( This[i]!== arr[arr.length-1]) {Arr.push ( This[i])}}returnarr; }           varArrnew =Arr.concat (arr1). Arrunique () Console.log (arrnew)//array de-weight 2           varR=[], arr= [' Apple ', ' strawberry ', ' banana ', ' pear ', ' apple ', ' orange ', ' orange ', ' strawberry '];  for(vari=0,len=arr.length;i<len;i++){                    //if the elements in the ARR array are not found in the new array, the array elements are stored in the new array                    if(R.indexof (Arr[i]) ===-1) {R.push (arr[i]); }                }           /*a truncated array of slice arrays. Slice (start index, end index)*/Console.log (Arr.slice (3))//intercept from index value 3 to lastConsole.log (Arr.slice (0,3))//intercept from 1 to 3 of index valuesConsole.log (Arr.slice (-2))//negative number after several           /*array. Splice (start index value, delete several, replace content 1, replace content 2, ...) It's a little similar to slice .*/Console.log (Arr.splice (3))//intercept from index value 3 to lastConsole.log (arr) console.log (Arr.splice (0,3))//Delete 3 starting from index 0Console.log (arr) console.log (arr1) Console.log (Arr1.splice (' A ', ' B ')//Delete 2 from index 1 and replace deleted content with ' A ', ' B 'Console.log (arr1)/*array. IndexOf (Element) (left to right) array contains elements, if included, returns the corresponding index, if not included, returns a 1 array. lastIndexOf ( Element) (from right to left)*/Console.log (Arr1.indexof (2))//index:0Console.log (Arr1.indexof (990))//-1; not found.Console.log (Arr1.lastindexof (2))//index:0Console.log (Arr1.lastindexof (990))//-1; not found.            /*Array Iterations*/            /*array. Map (function (item,index,array) {return NEWARR})*/                    varSporter =[{name:' AA ', Ishell:NULL},{Name:' BB ', Ishell:NULL},{Name:' CC ', Ishell:true                                                }]; varARR2 = []; Sporter.map (function(Item,index,array) {if(Item.ishell) {Arr2.push (item.name); }}) Console.log (ARR2)//ES6 notationSporter.map (item = {                     if(Item.ishell) {Arr2.push (item.name); }}) Console.log (ARR2)/*Array. filter (function (Item,index,array) {return NEWARR}) returns an array that matches the condition of the element true /c7>*/            vararr = Sporter.filter (item = {returnItem.ishell}); Console.log (arr)vararr = sporter.filter (item =Item.ishell); Console.log (arr) Console.log ('-1 ' = =false)//false            //array de-weight 3            varARR3= [1,2,3,2,3,5,6,6]; vararr = Arr3.filter (function(item,index,self) {Console.log (item+ '----------------' +Self )returnSelf.indexof (item) = =index;            }); Console.log (arr)/*each item in the every () array runs the given function, and returns TRUE if the function returns true for each item;             Some () runs the given function for each item in the array, and returns True if the function returns true for either item; */            vararr = Sporter.every (function(item) {returnItem.ishell; }) Console.log (arr)vararr = Sporter.some (function(item) {returnItem.ishell; }) Console.log (arr)/*ForEach () and for loop, no return value*/            vararr = Sporter.foreach (function(item) {Item.sex= ' Man '}) Console.log (arr)//because foreach has no return value, it is undefined            /*Array. redval (A, B) This method is generally used in cumulative accumulation, practical skills have not yet been found. For example, the number array is summed, and the string array is connected*/            //Array summation, concatenation of arrays            varsum=0,arr=[1,2,3,4,5,6]; Sum= Arr.reduce (function(A, b) {returnA +b; })            /*the array. Find (val) method returns the first element of an array that passed a test condition (function) that meets the criteria. */            varARR4= [1,2,3,4]; varA = Arr4.find (function(val) {returnVal > 2; }) Console.log (a)/*Array. FindIndex (val) FindIndex: The method returns the first element position of the array that passed a test condition (function) that meets the criteria. Returns 1 if it does not exist, and returns the first index that meets the criteria if a condition exists;*/           varA = Arr4.findindex (function(val) {returnVal > 4; }) Console.log (a)//-1           /*emptying of the array*/           varARR5 = [' A ', ' B ', ' C ', 1]; Arr5.length= 0;//pseudo array cannot be emptiedConsole.log (ARR5) Arr5.splice (0);//intercept from index 0 to last, pseudo-array cannot be emptiedConsole.log (ARR5); ARR5= [];//recommended use, Pseudo-array available           //Pseudo-array: is a long image array, but does not have an array of methods, can not add delete elementsFN (111,222,333); functionfn () {arguments.length= 0;//cannot be emptiedconsole.log (arguments); Arguments.splice (0) Console.log (arguments); //Errorarguments=[] console.log (arguments)}</script>

All right, there's something you need to know.

JS in the array of those things ~

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.