How to manipulate data:
1 vararr = [1,2,3,4,5];2 3Arr.push (6,7);//You can add a sequence (1+) to the last digit of the array, and return the length of the array. 4 5Arr.pop ();//deletes the last digit of the array. and returns the contents of that array. 6 7Arr.shift ();//deletes the No. 0 bit of the array and returns the contents of that array. 8 9Arr.unshift ( -1,0);//adds a sequence of numbers from the No. 0 bit of the array and returns the length of the array. Ten OneArr.join (");//Separates each column array by the specified symbol or by a non-set symbol (an empty string).
Ways to manipulate strings:
Implement push, pop, shift, and unshift methods with splice
1 vararr = [3,2];2 varArropts= (function(){3 4 functionOarray (arr) {5 6 if(Array.isarray (arr)) {7 return true;8}Else{9 return false;Ten } One A } - functionOpush (arr) { - the if(!Oarray (arr)) { - return"Not an array! "; - } - + for(vari=1;i<arguments.length;i++){ -Arr.splice (arr.length,0, Arguments[i]); + } A at returnarr.length; - } - - functionOPop (arr) { - - if(!Oarray (arr)) { in return"Not an array! "; - } to + returnArr.splice (arr.length-1,1) [0]; - } the * functionOunshift (arr) { $ if(!Oarray (arr)) {Panax Notoginseng return"Not an array! "; - } the if(arguments.length>=2){ + for(vari = arguments.length-1;i>0;i--){ AArr.splice (0,0, Arguments[i]); the } + } - returnarr.length; $ } $ - functionOshift (arr) { - if(!Oarray (arr)) { the return"Not an array! "; - }Wuyi returnArr.splice (0,1) [0]; the } - Wu return { - Opush:opush, About Opop:opop, $ Ounshift:ounshift, - Oshift:oshift - } - A})();
Stitching string Operations:
1 varProd = {name: ' Ladies ', styles:[' cropped ', ' Winter ', ' Spring '] };2 3 functionGETTPL (data) {4 5 varHTML =[]; Html.push (' <dl class= ' product ' > ');6 7Html.push (' <dt> ' +data.name+ ' </dt> ');8 9 for(varI =0;i<data.styles.length;i++){Ten OneHtml.push (' <dd> ' +data.styles[i]+ ' </dd> '); A - } - theHtml.push (' </dl> ')); - - returnHtml.join (' '); - + } - + varresult =gettpl (prod); A atConsole.log (result);//
Write a find function that implements the following functions
1 vararr = [' Test ', 2, 1.5,false];2 functionFind (Arr,ele) {3 for(vari=0;i<arr.length;i++){4 if(Arr[i] = = =ele) {5 returni;6 } 7 }8 return-1;9 }TenFind (arr, "test")//0 OneFind (arr, 2)//1 AFind (arr, 0)//-1
Write a function to filter the array, not the elements of the number.
1 varArr=["A", 1, "B", 2];2 3 functionFilternumeric (arr) {4 varFooarr = [];5 for(vari=0;i<arr.length;i++){6 if(typeofArr[i] = = = ' Number '){7 Fooarr.push (Arr[i]);8 }9 }Ten returnFooarr; One}
JavaScript Basics--arrays, string manipulation, math functions