Vue——数组操作

数组操作

点击按钮实现排序功能

<div id="app">
			<button @click="sortGoods">排序</button>
			<ul>
				<li v-for="good of goods">
					{{good.name}}---{{good.price}}---{{good.count}}
				</li>
			</ul>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el:"#app",
				data:{
					goods:[
						{
							name:'ipone',
							price:5000,
							count:5
						},
						{
							name:'华为',
							price:3000,
							count:8
						},
						{
							name:'小米',
							price:2000,
							count:15
						}
					]
				},
				methods:{
					sortGoods:function(){
						this.goods = this.goods.sort(function(x,y){
							return x.price-y.price
						});
					}
				}
			})
		</script>

点击按钮实现筛选功能

<div id="app">
			<button @click="filterGoods">筛选</button>
			<ul>
				<li v-for="(good,index) in goods">
					名称:{{good.name}}---价格: {{good.price}}---数量: {{good.count}}
				</li>
			</ul>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el:"#app",
				data:{
					goods:[
						{
							name:'ipnone',
							price:8000,
							count:12
						},
						{
							name:'华为',
							price:5000,
							count:5
						},
						{
							name:'vivo',
							price:3000,
							count:20
						},
						{
							name:'小米',
							price:3000,
							count:50
						},
						{
							name:8848,
							price:10000,
							count:2
						}
						
					]
				},
				methods:{
					filterGoods:function(){
						this.goods = this.goods.filter(function(element){
							if(element.price>5000){
								return element;
							}
						})
					}
				}
				
			})
		</script>

点击按钮实现移除功能

<div id="app">
			<ul>
				<li v-for="(good,index) in goods" :key="good.name">
					名称:{{good.name}}---价格: {{good.price}}---数量: {{good.count}}
						<button @click="deleteGood(index)">移除</button>
				</li>
			</ul>
		
		
		</div>
		<script type="text/javascript">
				var app = new Vue({
				el:"#app",
				data:{
					goods:[
						{
							name:'ipnone',
							price:8000,
							count:12
						},
						{
							name:'华为',
							price:5000,
							count:5
						},
						{
							name:'vivo',
							price:3000,
							count:20
						},
						{
							name:'小米',
							price:3000,
							count:50
						},
						{
							name:8848,
							price:10000,
							count:2
						}
						
					]
				},
				methods:{
					deleteGood:function(index){
						this.goods.splice(index,1);
					}
					
				}
				})
		</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值