JavaScript Array-String-Math Function
Mar 08, 2018 pm 03:24 PMThis time I will bring you JavaScript array-string-mathematical function, What are the precautions for using JavaScriptarray-string-mathematical function , the following is a practical case, let’s take a look.
The push() method adds one or more elements to the end of the array and returns the new length of the array (length attribute value).
The pop() method deletes the last element in an array and returns this element.
The shift() method deletes the first element of the array and returns this element. This method changes the length of the array.
The unshift() method adds one or more elements to the beginning of the array and returns the new length value of the array.
The join() method concatenates all elements in the array into a string.
**split() **The method splits a String object into a string array by splitting the string into substrings.
Definition and usage
splice() method is used for insertion and deletion Or replace elements of an array.
Syntax
arrayObject.splice(index,howmany,element1,.....,elementX)
index Required. Specifies where to add/remove elements. This parameter is the index of the array element to start inserting and/or deleting, and must be a number.
howmany Required. Specifies how many elements should be removed. Must be a number, but can be "0". If this parameter is not specified, all elements starting from index to the end of the original array are deleted. element1 optional. Specifies the new element to be added to the array. Insertion begins at the index pointed to by index.
elementX Optional. Several elements can be added to the array.
Return value
If an element is deleted from arrayObject, the array containing the deleted element is returned.
splice->push var a = [1,2,3,4,5] var b = [1,2,3,4,5] console.log(a); console.log(b); a.push(6); b.splice(5,1,6); console.log(a); console.log(b); splice->pop var a = [1,2,3,4,5] var b = [1,2,3,4,5] console.log(a); console.log(b); a.pop(); b.splice(4,1); console.log(a); console.log(b); splice->shift var a = [1,2,3,4,5] var b = [1,2,3,4,5] console.log(a); console.log(b); a.shift(); b.splice(0,1); console.log(a); console.log(b); splice->unshift var a = [1,2,3,4,5] var b = [1,2,3,4,5] console.log(a); console.log(b); a.unshift(-1); b.splice(0,0,-1); console.log(a); console.log(b);
var prod = { name: '女装', styles: ['短款', '冬季', '春装'] };function getTpl(data){//todo...};var result = getTplStr(prod); //result为下面的字符串 <dl class="product"> <dt>女装</dt> <dd>短款</dd> <dd>冬季</dd> <dd>春装</dd> </dl>
var prod = { name: '女装', styles: ['短款', '冬季', '春装'] }; function getTplStr(data){ var htmls = []; htmls.push('<dl class="product">','<dt>'+data,name+'<dt>'); for(i=0;i<data.styles.length;i++){ htmls.push('<dd>'+data.styles[i]+'<dd>') } htmls.push('<dl>'); var htmls = htmls.join('') return htmls }; var result = getTplStr(prod); //result为下面的字符串 console.log(result)
var arr = [ "test", 2, 1.5, false ] find(arr, "test") // 0 find(arr, 2) // 1 find(arr, 0) // -1
var arr = [ "test", 2, 1.5, false ] var find = function(a,b){ console.log(a.indexOf(b)) } find(arr, "test") // 0 find(arr, 2) // 1 find(arr, 0) // -1
arr = ["a", 1,3,5, "b", 2]; newarr = filterNumeric(arr); // [1,3,5,2]
Method 1:
arr = ["a", 1,3,5, "b", 2]; var filterNumberic = function(data){ var a = []; for(i=0;i<data.length;i++){ if(typeof data[i] === 'number'){ a.push(data[i]); } } return a }
console.log(newarr)
Method 2:
arr = ["a", 1,3,5, "b", 2]; function isNumber(element) { return typeof element === 'number'; } var newarr = arr.filter(isNumber) console.log(newarr)
var obj = {className: 'open menu'}addClass(obj, 'new') // obj.className='open menu new'addClass(obj, 'open') // 因为open已经存在,此操作无任何办法addClass(obj, 'me') // obj.className='open menu new me'console.log(obj.className) // "open menu new me" removeClass(obj, 'open') // obj.className='menu new me' removeClass(obj, 'blabla') // 不变
var obj = {className: 'open menu'}var addClass = function(a,b){var name = a.className.split(" ");if(name.indexOf(b) === -1) {name.push(b);}else{console.log("因为"+b+"已经存在,此操作无任何办法");}a.className = name.join(" ");console.log('obj.className='+a.className);}var removeClass = function(a,b){var name = a.className.split(" ");if(name.indexOf(b) !== -1){name.splice(name.indexOf(b),1)a.className = name.join(" ");console.log('obj.className='+a.className)}else{console.log('不变')}} addClass(obj, 'new') // obj.className='open menu new' addClass(obj, 'open') // 因为open已经存在,此操作无任何办法 addClass(obj, 'me') // obj.className='open menu new me' console.log(obj.className) // "open menu new me" removeClass(obj, 'open') // obj.className='menu new me' removeClass(obj, 'blabla') // 不变
camelize("background-color") == 'backgroundColor' camelize("list-style-image") == 'listStyleImage'
function camelize(string){ return string.replace(/-/g,'') } console.log(camelize("background-color")) camelize("background-color") == 'backgroundColor' camelize("list-style-image") == 'listStyleImage'
arr = ["a", "b"]; arr.push( function() { alert(console.log('hello hunger valley')) } ); arrarr.length-1 // ?
arr = ["a", 1,3,4,5, "b", 2]; //对原数组进行操作,不需要返回值 filterNumericInPlace(arr); console.log(arr) // [1,3,4,5,2]
arr = ["a","d", 1,3,4,5, "b", 2]; //对原数组进行操作,不需要返回值 function filterNumericInPlace(data){ for(i=0;i<data.length;i++){ if(typeof data[i] === 'string'){ data.splice(i,1); i--;//splice指针减少1,否则获取不了数组中全部元素。 } } } filterNumericInPlace(arr); console.log(arr) // [1,3,4,5,2]
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 ] ageSort(people) // [ bob, mary, john ]
Method 1:
function ageSort(arr){ arr.sort(function(a,b){return a.age-b.age}) return 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 ] ageSort(people) // [ bob, mary, john ] console.log(ageSort(people))
function ageSort(a){ for(i=0;i<a.length;i++){ for(j=i+1;j<a.length;j++){ if(a[i].age-a[j].age>0){ var b = a[i]; a[i] = a[j]; a[j] = b; } } } return a } 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 ] ageSort(people) // [ bob, mary, john ] console.log(ageSort(people))
function isNumeric (el){return typeof el === 'number';}arr = ["a",3,4,true, -1, 2, "b"] arr = filter(arr, isNumeric) ; // arr = [3,4,-1, 2], 过滤出数字 arr = filter(arr, function(val) { return val > 0 }); // arr = [2] 过滤出大于0的整数
##. #
function filter(data,callback){return data.filter(callback)} function isNumeric (el){ return typeof el === 'number'; } arr = ["a",3,4,true, -1, 2, "b"] arr = filter(arr, isNumeric) ; // arr = [3,4,-1, 2], 过滤出数字 console.log(arr) arr = filter(arr, function(val) { return val > 0 }); // arr = [2] 过滤出大于0的整数 console.log(arr)
String
Write a ucFirst function that returns the character whose first letter is uppercase
ucFirst("hunger") == "Hunger"
Code:
function ucFirst(string){ return string[0].toUpperCase()+string.slice(1); } console.log(ucFirst("hunger")) ucFirst("hunger") == "Hunger"
Write a function truncate(str, maxlength). If the length of str is greater than maxlength, str will be truncated to maxlength and added..., such as:
truncate("hello, this is hunger valley,", 10)) == "hello, thi..."; truncate("hello world", 20)) == "hello world"
Code:
function truncate(str,maxlength){ if(str.length>maxlength){ var sub = str.substring(maxlength) str = str.replace(sub,'...'); } return str; } console.log(truncate("hello, this is hunger valley,", 10)); truncate("hello, this is hunger valley,", 10) == "hello, thi..."; truncate("hello world", 20) == "hello world"
Mathematical function
Write a function limit2, keep the number to two decimal places, and round, such as:
var num1 = 3.456 limit2( num1 ); //3.46 limit2( 2.42 ); //2.42
Code:
var num1 = 3.456 function limit2(data){ var num = Math.round(data*100); return num/100 } limit2( num1 ); //3.46 limit2( 2.42 ); //2.42 console.log(limit2(num1)); console.log(limit2(2.42)); console.log(limit2(-1.15555555))
Write a function to get the random number between min and max, including min but not max. Code:
function fun(min,max){ return min+Math.random()*(max-min) } console.log(fun(5,10))
Write a function to get the random number between min and max. Random integers, including min and max. Code:
function fun(min,max){ return Math.Round(min+Math.random()*(max-min)) } console.log(fun(5,10))
Write a function to obtain a random array. The elements in the array are of length len, the minimum value is min, and the maximum value is max( Random numbers including).Code:
function fun(min,max,leng){ var arr = [] for(i=0;i<leng;i++){ var value = max-Math.random()*(max-min) arr.push(value) } return arr } console.log(fun(5,10,6))
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Related reading:
JS closures and timersJS Dom and event summaryThe above is the detailed content of JavaScript Array-String-Math Function. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recommended: Excellent JS open source face detection and recognition project

Detailed explanation of the method of converting int type to string in PHP

How to check if a string starts with a specific character in Golang?

How to determine whether a Golang string ends with a specified character

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions
