当前位置:网站首页>Basic use of calculation attributes
Basic use of calculation attributes
2022-07-28 01:05:00 【If I become a demon, Buddha, I'm a program maniac】
Method of calculating basic attributes computed Calculate attributes the following code demonstrates
html Code
<div id="app">
<h2>{
{xing}}</h2>// This is the insertion method
<h2>{
{ming}}</h2>
<h2>{
{names()}}</h2><-- Use methods Return value -->
<h2>{
{uname}}</h2><-- At this time computed Calculate the return value of the attribute -->
</div>js Code
const vm=new Vue({
el:'#app',// mount
data(){// data
return{
xing:" Zhang ",
ming:' 3、 ... and ',
}
},
methods:{// Use method return value
names(){
return this.xing+this.ming
}
},
computed:{// Method of calculating basic properties There must be a return value
uname(){
return this.xing+this.ming
}
}
})The method of calculating complex attributes is shown in the following code demonstration
html Code
<div id="app">
<h2> Commodity price :{
{tatabprice}}</h2>
</div>
js Code
reduce Method to get the value
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
/reduce Method
tatabprice(){
return this.arr.reduce((a,b)=>{
return a+b.price
},0)
}
}
})
for...in Method to get the value
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
let num=0;
for(let index in this.arr){
num+=this.arr[index].price;
}
return num
}
})
for...of Method
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
let num=0;
for(let item of this.arr){
num+=item.price;
}
return num
}
})
forEach Method
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
let num=0;
this.arr.forEach(item=>{
num+=item.price
})
return num
}
})
map Method
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
let num=0;
this.arr.map(item=>{
num+=item.price
})
return num
}
})
filter Method
const vm=new Vue({
el:'#app',
data(){
return{
arr:[
{
id:1,
commodity:" watermelon ",
price: 8.9,
},{
id:2,
commodity:' pumpkin ',
price: 6.5,
},{
id:3,
commodity:'',
price: 7.8,
},{
id:4,
commodity:' Wax gourd ',
price: 10.2,
}
]
}
},
computed:{
let num=0;
this.arr.filter(item=>{
num+=item.price
})
return num
}
})
边栏推荐
- Recurrence of fastjson historical vulnerabilities
- [proteus simulation] 51 single chip microcomputer washing machine simulation control program
- Data analysis: disassembly method (details)
- Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
- DEMO:测试接口短时间内接收重复数据创建单据
- Shell系统学习之循环结构
- LED, nixie tube and key of single chip microcomputer
- [BuildRelease Management]Parabuild
- Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
- Jerry caused other messages to accumulate in the message pool [article]
猜你喜欢

Basic operations of MySQL database (I) --- Based on Database
![Leetcode:1997. the first day after visiting all rooms [jump DP]](/img/6e/52d5871a11d1b27e673112a8245b28.png)
Leetcode:1997. the first day after visiting all rooms [jump DP]

DEMO:测试接口短时间内接收重复数据创建单据

《KMP复习 + AC自动机》前传

接口测试实战项目02:读懂接口测试文档,上手操练

KMP review + AC automata Prequel

Wavelet transform learning notes
![[CruiseControl]Build Result JSP](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[CruiseControl]Build Result JSP

立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!

福特SUV版“野马”正式下线,安全、舒适一个不落
随机推荐
UML类图的六大关系,最佳学习理解方式
MySQL limit usage and large paging problem solving
The most detailed summary of common English terms in the chip industry (quick grasp of graphics and text)
Examples of application of JMeter in performance testing
激活最大化
Maximize activation
startUMl
Oracle错误: ORA-01722 无效数字
Read cmake in one article
进程与进程调度
leetcode:1997. 访问完所有房间的第一天【跳跃dp】
函数相关知识
Recommended system model: DSSM twin tower model for embedded recall
Recommended system - indicators: CTR, CVR
Cross desktop web container evolution
推荐系统-离线召回:u2tag2i、icf
Data visualization - White Snake 2: black snake robbery (3)
《KMP复习 + AC自动机》前传
Impact of privilege changes on existing connections
Array related knowledge