This article will explain js arrays, strings and functions.
What are the functions of push, pop, shift, unshift, join, and split in the array method?
push: Add an element at the end of the array, the syntax is array.push (the element to be added) ;, the return value is the length of the array
pop: delete the last element of the array, the syntax is array.pop( ); the return value is the name of the deleted element
shift: delete the first element of the array , the syntax is array.shift(); the return value is the name of the deleted element
unshift: add an element at the beginning of the array, and the subsequent elements are offset backwards, the syntax is array.unshift (the element to be added); , the return value is the length of the array
join: Connect the array into a string, without modifying the original array, the syntax is array.join(), the return value is the string after the connection is completed
split : Separate the string and turn it into an array without modifying the original string. The syntax is string.split('separator');
Code:
Use splice to implement push and pop , shift, unshift method
splice implements push:
new1
[91, 3, 2, 1, 34, 5] //Element of new1 array new1.splice(new1. length,0,91) //new1.length represents after the last digit of the array, 0 is the keyword to add, 91 is the element to be added []
new1
[91, 3, 2, 1, 34, 5, 91] //Successfully added element 91 at the end of array new1
Use splice to implement pop:
new1
[91, 3, 2, 1, 34 , 5, 9, 91] //Element of new1 array new1.splice(new1.length-1,1) //new1.length represents the last digit of the array, 1 is the length [91]
new1
[91, 3, 2, 1, 34, 5, 9] //Successfully delete the last element 91
splice implements shift:
new1
[ 91, 3, 2, 1, 34, 5, 645] //Elements of new1 array new1.splice(0,1) //0 represents the array subscript index number, 1 represents the number of deletions [91] //Return to delete Element new1 ##[3, 2, 1, 34, 5, 645] //Elements of new1 array new1.splice(0,0,91) //The first 0 represents the array subscript index number, and the second 0 is the key Word addition, 91 is the element to be added []
new1
at the first position of the array
Use an array to splice the following string
var prod = { name: '女装', styles: ['短款', '冬季', '春装'] };function getTp(data){ var new1 = prod.name; var new2 = prod.styles; var arrey =[]; arrey.push('
var arr = [ "test" , 2 , 1.5 , false ]function find(arr,add){ for(var i = 0;i < arr.length; i++){ if(add==arr[i] && add !== 0){ return console.log(i) } } console.log(-1) } find(arr, "test") // 0find(arr, 2) // 1find(arr, 0) // -1
arr = ["a", 1,3,5, "b", 2];var arre = [];function filterNumeric(arr){ for(var i= 0; i< arr.length;i++){ if(typeof arr[i]==='number'){ arre.push(arr[i]) } } console.log(arre); } filterNumeric(arr) //[1, 3, 5, 2]
var obj = { className: 'open menu'}; var shu = obj.className.split(" "); function addClass(obj,nano){ for(var i = 0;i< shu.length; i++) { if(shu[i] === nano) { return console.log("因为"+nano+"已经存在,此操作无任何办法"); } } shu.push(nano); //console.log(shu); obj.className = shu.join(" "); console.log(obj); } addClass(obj, 'new'); //Object {className: "open menu new"}addClass(obj, 'open'); //因为open已经存在,此操作无任何办法addClass(obj, 'me'); // Object {className: "open menu new me"}console.log(obj.className); // open menu new mefunction removeClass(obj,habo){ //console.log(shu) for(var i = 0;i Copy after login
function camelize(lama){ var lala = lama.split("-"); //["list", "style", "image"] var a =[lala[0]]; for(var i =1; i Copy after login
arr = ["a", "b"]; arr.push( function() { alert(console.log('hello hunger valley')) } ); arr[arr.length-1]() //
arr = ["a", 1 , 3 , 4 , 5 , "b" , 2];function filterNumericInPlace(arr){ for(var i= 0; 0< arr.length; i++) { if( typeof arr[i] !== 'number'){ arr.splice(i,1) } } console.log(arr); // [1,3,4,5,2]} filterNumericInPlace(arr);
var john = { name: "John Smith", age: 23 };var mary = { name: "Mary Key", age: 18 };var bob = { name: "Bob-small", age: 6 };var people = [ john, mary, bob ];function ageSort(data){ data.sort(function (a,b){ return a.age-b.age; }) console.log(data); } ageSort(people); // [ bob, mary, john ]
function isNumeric (el){ return typeof el === 'number'; } arr = ["a",3,4,true, -1, 2, "b"];function filter(arr, func){ for(var i =0;i0});console.log(arr); [3, 4, -1, 2] [3, 4, 2]
function ucFirst(daho){ var add= daho[0].toUpperCase(); daho=daho.replace(daho[0],add) console.log(daho); } ucFirst("hunger")"Hunger"
function truncate(str, maxlength){ if(str.length-1>maxlength){ add = str.substr(0,maxlength); console.log(add+"..."); }else{ return console.log(str); } } truncate("hello, this is hunger valley,", 10) truncate("hello world", 20)"hello, thi...""hello world"
var num1 = 3.456;function limit2(num){ num=Math.round(num*100)/100; console.log(num); } limit2(num1) limit2(2.42)3.462.42
function habo(min,max){ console.log(Math.random()*(min-max)+max) } habo(5,15)
function habo(min,max){ add = Math.random()*(min-max)+max; console.log(Math.round(add)); } habo(5,12)
function damo(len,min,max){ arr=[]; len == arr.length; for(var i =0;i Copy after login
About JS time objects and recursion issues
Some basic knowledge of CSS stylesExplanation on JS timer and closure issues
The above is the detailed content of JS arrays, strings and mathematical functions. For more information, please follow other related articles on the PHP Chinese website!