当前位置:网站首页>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
}
})
边栏推荐
- Jerry's PWM setting and PWM IO selection [chapter]
- 分支和循环语句题目练习
- 计算属性的基本使用
- Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
- [CruiseControl]Build Result JSP
- LED, nixie tube and key of single chip microcomputer
- Recommended system - fine tuning model: xdeepfm
- Byte flybook Human Resource Kit three sides
- SRv6初登场
- 多线程及多线程程序的编写
猜你喜欢

Set data constructor

Rongyun IM & RTC capabilities on new sites

110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe

激活最大化

Cross desktop web container evolution

startUMl

The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer

函数相关知识

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

Solve maze problem recursively
随机推荐
融云 IM & RTC 能力上新盘点
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
IP address & subnet mask
Map set
接口测试实战项目02:读懂接口测试文档,上手操练
0-1背包问题
Storage of deep planing data in memory
[original] [crawler series] briefly get the most popular topics, related topics and description information
红队大杀器 Behinder_v4.0(冰蝎4.0)
Rancher2.6 monitoring grafana docking LDAP
Logic of automatic reasoning 07 - predicate calculus
Read cmake in one article
为华为打造无美系设备的产线,台积电三星能做到吗?
范德蒙德卷积 学习笔记
Leetcode:1997. the first day after visiting all rooms [jump DP]
Interface test practical project 02: read interface test documents and practice
DC motor winding parameters
Volkswagen China invested 8billion yuan and became the largest shareholder of GuoXuan high tech
Can TSMC Samsung build a production line without American equipment for Huawei?
Recommended system model: DSSM twin tower model for embedded recall