普通for循环
<script>
const app=new Vue({
el : ‘#app‘,
data: {
prices: [85, 25.25, 45, 36.1]
},
computed: {
totalPrice() {
let total = 0
for(let i=0;i<this.prices.length;i++){
total += this.prices[i]
}
return total
}
}
})
</script>
for(let i in this.prices)
<script>
const app=new Vue({
el : ‘#app‘,
data: {
prices: [85, 25.25, 45, 36.1]
},
computed: {
totalPrice() {
let total = 0
for(let i in this.prices){
total += this.prices[i]
}
return total
}
}
})
</script>
for(let i of this.prices)
<script>
const app=new Vue({
el : ‘#app‘,
data: {
prices: [85, 25.25, 45, 36.1]
},
computed: {
totalPrice() {
let total = 0
for(let i of this.prices){
total += i
}
return total
}
}
})
</script>
JavaScript数组filter函数的使用
<script>
const nums = [10, 25, 5, 8, 422, 50]
let newNums = nums.filter(function (n) {
return n<100
})
</script>
JavaScript数组map函数的使用
注意:map() 方法按照原始数组元素顺序依次处理元素。
注意: map() 不会对空数组进行检测。
注意: map() 不会改变原始数组。
<script>
const nums = [10, 25, 5, 8, 422, 50]
let newNums = nums.map(function (n) {
return n*2
})
</script>
JavaScript数组reduce函数的使用
arr.reduce(callback,[initialValue])
callback (执行数组中每个值的函数,包含四个参数)
previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
currentValue (数组中当前被处理的元素)
index (当前元素在数组中的索引)
array (调用 reduce 的数组)
initialValue (作为第一次调用 callback 的第一个参数previousValue的初始化值。)
<script>
const nums = [10, 25, 5, 8]
let total = nums.reduce(function (preValue, n) {
return preValue + n
}, 0)
//第一次 preValue:0 n:10
//第二次 preValue:10 n:25
//第三次 preValue:35 n:5
//第四次 preValue:40 n:8
//48
//箭头函数
let all = nums.filter(n => n<100).map(n => n*2).reduce((preVaule, n) => preVaule+n)
</script>
原文:https://www.cnblogs.com/tang321/p/14544392.html