字符串、对象、数组操作方法、json方法
2018-06-24 00:59:22来源:未知 阅读 ()
1.字符串操作方法
var str='kaivon';
var s1=str.charAt(1);
console.log(s1); //a
var s2=str.charAt();
console.log(s2); //k
var s3=str.charAt(10);
console.log(s3); //空
var str='kaivon';
var s1=str.charCodeAt(1);
console.log(s1); //97
var s2=str.charCodeAt();
console.log(s2); //107
var s3=str.charCodeAt(10);
console.log(s3); //NaN
var str='kaivaokn';
var s1=str.indexOf('a');
console.log(s1); //1
var s2=str.indexOf('k',1);
console.log(s2); //6
var s3=str.indexOf('b');
console.log(s3); //-1
var str='kaivokn';
var s1=str.lastIndexOf('a');
console.log(s1); //1
var s2=str.lastIndexOf('k',5);
console.log(s2); //5
var s3=str.lastIndexOf('b');
console.log(s3); //-1
* 与slice的区别* 1、slice起始位置不能大于结束位置,而substring起始位置可以大于结束位置 (会自动调大小)* 2、slice可以放负数,而substring是不能放负数的
var str='kaiovn';
var s1=str.slice();
console.log(s1); //kaiovn
var s2=str.slice(3);
console.log(s2); //ovn
var s3=str.slice(2,4);
console.log(s3); //io
var s4=str.slice(-3,-2);
console.log(s4); //o
console.log(str); //kaiovn
var str='kaivon';
var s1=str.split();
console.log(s1); //["kaivon"]
var s2=str.split('');
console.log(s2); //["k", "a", "i", "v", "o", "n"]
var s3=str.split(' ');
console.log(s3); //["kaivon"]
var s4=str.split('a');
console.log(s4); //["k", "ivon"]
var s5=str.split('i',1);
console.log(s5); //["ka"]
console.log(str); //kaivon
var str='kaivon';
var s1=str.substr();
console.log(s1); //kaivon
var s2=str.substr(2);
console.log(s2); //ivon
var s3=str.substr(3,2);
console.log(s3); //vo
console.log(str); //kaivon
var str='KaiVoN';
var s1=str.toLowerCase();
console.log(s1); //kaivon
console.log(str); //KaiVoN
var str='kaivon';
var s1=str.toUpperCase();
console.log(s1); //KAIVON
console.log(str); //kaivon
var str=' kaivon ';
var s1=str.trim();
console.log(s1); //kaivon
console.log(str); // kaivon
var j1="[1,2,3]";
var j2='{"name":"老王","age":30}';
console.log(j1,typeof j1); //[1,2,3] string
console.log(j2,typeof j2); //{"name":"老王","age":30} string
var j1="[1,2,3]";
var j2='{"name":"老王","age":30}';
var obj1=JSON.parse(j1);
console.log(obj1,typeof obj1); //[1, 2, 3] "object"
var obj2=JSON.parse(j2);
console.log(obj2,typeof obj2); //{name: "老王", age: 30} "object"
var j3='{name:"kaivon"}';
var obj3=JSON.parse(j3); //报错 key没有加引号
var j4='['aow','shw','dwn']';
console.log(JOSN.parse(j4)); //报错 因为元素只是单引号,需要改成双引号
var obj1=[1,2,3];
var obj2={"name":'老王',"age":30};
var obj3={name:'老王',"age":30};
var obj4=['ad','gf','fsd'];
console.log(JSON.stringify(obj4)); // ["ad","gf","fsd"]
var s1=JSON.stringify(obj1);
console.log(s1); //[1,2,3]
var s2=JSON.stringify(obj2);
console.log(s2); //{"name":"老王","age":30}
var s3=JSON.stringify(obj3);
console.log(s3); //{"name":"老王","age":30}
console.log(Math.ceil(0.1)); //1
console.log(Math.ceil(-10.8)); //-10
console.log(Math.ceil(10)); //10
console.log(Math.floor(0.1)); //0
console.log(Math.floor(-10.8)); //-11
console.log(Math.floor(10)); //10
console.log(Math.round(3.15)); //3
console.log(Math.round(4.5)); //5
console.log(Math.round(-10.87)); //-11
console.log(Math.abs(10)); //10
console.log(Math.abs(-11)); //11
console.log(Math.abs(5-7)); ///2
//0-10
console.log(Math.round(Math.random()*10));
//5-7
console.log(Math.round(Math.random()*(7-5) + 5));
var color=['red','green','blue','pink'];
var len=color.push('white');
console.log(color); //['red','green','blue','pink','white'];
console.log(len); //5
var color=['red','green','blue','pink'];
var len=color.unshift('white');
console.log(color); //['white','red','green','blue','pink']
console.log(len); //5
var color=['red','green','blue','pink'];
var del=color.pop();
console.log(del); //pink
console.log(color); //["red", "green", "blue"]
var color=['red','green','blue','pink'];
var del=color.shift();
console.log(del); //red
console.log(color); //["green", "blue", "pink"]
var color=['red','green','blue','pink'];
color.forEach(function(currentValue,index,array){
console.log(currentValue,index,array)
});
var arr=[4,2,7,8,3,1,10];
arr.sort(function(a,b){
return a-b;
})
console.log(arr); //[1, 2, 3, 4, 7, 8, 10]
var arr1=[4,2,7,8,3,1,10];
arr1.sort(function(a,b){
return b-a;
});
console.log(arr1); //[10, 8, 7, 4, 3, 2, 1]
//随机排序
var arr2=[4,2,7,8,3,1,10];
arr2.sort(function(){
return 0.5-Math.random();
})
console.log(arr2);
//没有参数
var arr3=[4,2,7,8,3,1,10];
arr3.sort();
console.log(arr3); //按照字符对应的unicode编码号
var color=['red','green','blue','pink'];
var a=color.reverse();
console.log(a); //["pink", "blue", "green", "red"]
console.log(color); //["pink", "blue", "green", "red"]
var color=['red','green','blue','pink'];
var newColor=color.concat('yellow');
console.log(newColor); //["red", "green", "blue", "pink", "yellow"]
console.log(color); //["red", "green", "blue", "pink"]
var newColor2=color.concat(['yellow','gold']);
console.log(newColor2); //["red", "green", "blue", "pink", "yellow", "gold"]
var newColor3=color.concat(1,2,3);
console.log(newColor3); //["red", "green", "blue", "pink", 1, 2, 3]
var color=['red','green','blue','pink'];
var newColor=color.join();
console.log(newColor); //red,green,blue,pink
console.log(color); //["red", "green", "blue", "pink"]
var new1=color.join('');
console.log(new1); //redgreenbluepink
var new2=color.join(' ');
console.log(new2); //red green blue pink
var new3=color.join('&');
console.log(new3); //red&green&blue&pink
var color=['red','green','blue','pink','yellow'];
var n1=color.slice();
console.log(n1); //["red", "green", "blue", "pink", "yellow"]
var n2=color.slice(2);
console.log(n2); //["blue", "pink", "yellow"]
var n3=color.slice(3,4);
console.log(n3); //["pink"]
console.log(color); //["red", "green", "blue", "pink", "yellow"]
var color=['red','green','blue','pink','yellow'];
var c1=color.splice();
console.log(c1); //[]
console.log(color); //["red", "green", "blue", "pink", "yellow"]
var c2=color.splice(3);
console.log(c2); //["pink", "yellow"]
console.log(color); //["red", "green", "blue"]
var color1=['red','green','blue','pink','yellow'];
var c3=color1.splice(1,3);
console.log(c3); //["green", "blue", "pink"]
console.log(color1); //["red", "yellow"]
var color2=['red','green','blue','pink','yellow'];
var c4=color2.splice(1,2,'white','black');
console.log(c4); //["green", "blue"]
console.log(color2); //["red", "white", "black", "pink", "yellow"]
var color3=['red','green','blue','pink','yellow'];
var c5='a,b,c';
var c6=color3.splice(3,2,c5);
console.log(c6); //['pink',yellow]
console.log(color3); //["red", "green", "blue", "a,b,c"]
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Unicode字符集和编码方式
- Bootstrap 滚动监听+小工具+Flex(弹性)布局+多媒体对象 2020-04-20
- JavaScript 数组,字符串,函数 2020-01-08
- 随机点名 2019-12-09
- audio标签以及audio对象 2019-10-16
- CSS截断字符串的实用方法 2019-10-12
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash