网站如何推广运营,app制作手机版,新开传奇手游新服网,wordpress安全锁一、数组和字符串之间的转换方式 1#xff09;将字符串切割成字符串数组—stringObject.split(separator, howmany) seperator-----字符串、正则表达式#xff0c;必需 howmany------指定返回的数组的最大长度#xff0c;可省略#xff0c;省略后全量返回 源代码 var str将字符串切割成字符串数组—stringObject.split(separator, howmany) seperator-----字符串、正则表达式必需 howmany------指定返回的数组的最大长度可省略省略后全量返回 源代码 var str1.2.3.4.5;arrstr.split(.);
console.log(arr); 运行结果 2把数组中的所有元素放入一个字符串-----arrayObject.join(separator) seperator-----可选指定要使用的分隔符如果省略该参数则使用逗号作为分隔符 源代码 var arr[1,2,3,4,5]; //隐式创建数组
console.log(arr);str1arr.join(); // 无分隔符
console.log(str1);str2arr.join(); //省略分隔符
console.log(str2);运行结果 二、数组的一些方法 1pop() arrayObject.pop()删除 arrayObject 的最后一个元素把数组长度减 1并且返回它删除的元素的值。如果数组已经为空则 pop() 不改变数组并返回 undefined 值 源代码 var arr[1,2,3,4,5]; //隐式创建数组
console.log(arr前:, arr);returnValuearr.pop()
console.log(arr后:, arr);
console.log(returnValue:, returnValue);运行结果 2push() arrayObject.push(newelement1,newelement2,....,newelementX)把它的参数顺序添加到 arrayObject 的尾部返回数组的新长度它直接修改 arrayObject而不是创建一个新的数组 源代码 var arr[1,2,3,4,5]; //隐式创建数组
console.log(arr前:, arr);returnValuearr.push(7,8,9)
console.log(arr后:, arr);
console.log(returnValue:, returnValue);运行结果 3shift() arrayObject.shift()删除 arrayObject 的第一个元素并返回第一个元素的值,数组是空的那么 shift() 方法将不进行任何操作返回 undefined 值不改变原数组 源代码 var arr[1,2,3,4,5]; //隐式创建数组
console.log(arr前:, arr);returnValuearr.shift()
console.log(arr后:, arr);
console.log(returnValue:, returnValue);运行结果 4unshift() arrayObject.unshift(newelement1,newelement2,....,newelementX) 把它的参数顺序添加到 arrayObject 的开头并返回新的长度,改变原数组 源代码 var arr[1,2,3,4,5]; //隐式创建数组
console.log(arr前:, arr);returnValuearr.unshift(7,8,9)
console.log(arr后:, arr);
console.log(returnValue:, returnValue);运行结果