1 split(' ') 切割符
把字符串分割为数组 引号间用空格
<div class="app">
<h1>{{huan}}</h1>
</div>
<script>
new Vue({
el:'.app',
data:{
mag:'hell vue'
},
computed:{
huan:function(){
return this.mag.split(' ')
}
}
})
</script>
//输出结果为:[ "hell", "vue" ]
2 join 拼接符
<div class="app">
<h1>{{huan}}</h1>
</div>
<script>
new Vue({
el:'.app',
data:{
mag:'hell vue'
},
computed:{
huan:function(){
return this.mag.split(' ').join
}
}
})
</script>
//输出结果为:hell,vue
3 reverse 翻转
<div class="app">
<h1>{{huan}}</h1>
</div>
<script>
new Vue({
el:'.app',
data:{
mag:'hell vue'
},
computed:{
huan:function(){
return this.mag.split(' ').reverse
}
}
})
</script>
//输出结果为:[ "vue", "hell" ]
4 *** string***
把对象的值转为字符串
<button onclick="myFunction()">点击</button>
<script>
function myFunction(){
var ff= ["Banana", "Orange", "Apple", "Mango"];
var str = ff.toString();
var a=document.getElementById("demo");
a.innerHTML= str;
}
</script>
5 concat 用于连接两个或多个数组
<script type="text/javascript">
var a = [1,2,3];
document.write(a.concat(4,5));
</script>
//输出结果为:1,2,3,4,5
6 slice
从已有的数组中返回选定的元素
<script>
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
document.write(arr.slice(2,4) + "<br />")
</script>
//输出结果为:Thomas,James
7 splice('坐标下添加 '‘删除0个或一个’)
从数组中添加/删除项目,然后返回被删除的项目
<script>
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
arr.splice(2,0,"William")
document.write(arr)
</script>
//输出结果为:George,John,William,Thomas,James,Adrew,Martin
8 ***push() ***
向数组的末尾添加一个或多个元素,并返回新的长度。
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.push("James") )//输出结果为:4
document.write(arr)//输出结果为:George,John,Thomas,James
9 pop()
删除并返回数组的最后一个元素。
<script>
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.pop())
document.write(arr)
</script>
//输出结果为:Thomas
//输出结果为:George,John
10 unshift
向数组的开头添加一个或更多元素,并返回新的长度
<script type="text/javascript">
var arr = new Array()
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.unshift("William") + "<br />")
document.write(arr)
//输出结果为:
George,John,Thomas
4
William,George,John,Thomas
11 shift()
把数组的第一个元素从其中删除,并返回第一个元素的值。
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)
</script>
//输出结果为:
George,John,Thomas
George
John,Thomas