标签:class 方式 nbsp tostring body ring slice style 字符串
字面量方式创建:
var colors = [‘red‘, ‘green‘, ‘yellow‘];
使用构造函数的方式创建(使用new关键字对构造函数进行创建对象)
var colors2 = new Array();
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var color = []; 12 color[0] = ‘red‘; 13 color[1] = ‘green‘; 14 color[2] = ‘yellow‘; 15 console.log(color); 16 </script> 17 18 </body> 19 </html>
3.1 concat:把几个数组合并成一个数组
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var north = [‘北京‘, ‘山东‘, ‘天津‘]; 12 var south = [‘东莞‘, ‘深圳‘, ‘上海‘]; 13 var newCity = north.concat(south); 14 console.log(newCity); 15 </script> 16 17 </body> 18 </html>
3.2 join:将数组中的元素使用指定的字符串连接起来,他会形成一个新的字符串
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var str = score.join(‘*‘); 13 console.log(str); 14 </script> 15 16 </body> 17 </html>
3.3 将数组转换成字符串toString()
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var str = score.toString(); 13 console.log(str); 14 </script> 15 16 </body> 17 </html>
3.4 slice(start,end):返回数组的一段,左闭右开
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var score = [78, 89, 48, 20, 100]; 12 var nscore = score.slice(1, 4); 13 console.log(nscore); 14 </script> 15 16 </body> 17 </html>
3.5 pop:删除数组的最后一个元素,并返回删除的元素
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>流浪者</title> 6 7 </head> 8 <body> 9 10 <script> 11 var arr = [‘张三‘, ‘李四‘, ‘王文‘, ‘找六‘]; 12 var item = arr.pop(); 13 console.log(arr); 14 console.log(item); 15 </script> 16 17 </body> 18 </html>
标签:class 方式 nbsp tostring body ring slice style 字符串
原文地址:https://www.cnblogs.com/897463196-a/p/11456372.html