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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,可以使用`reactive`和`ref`来定义对象和数组。 对于对象,可以使用`reactive`来定义。例如: ```javascript import { reactive } from 'vue' const obj = reactive({ name: '张三', age: 18, job: '工程师' }) ``` 对于数组,可以使用`ref`来定义。例如: ```javascript import { ref } from 'vue' const arr = ref([1, 2, 3, 4]) ``` 需要注意的是,Vue3中的`ref`只能用于基本类型数据,如果要定义数组或对象类型数据,可以使用`reactive`,它会自动将对象或数组转换为代理对象,实现响应式。 参考文献: 从定义数据角度对比 [2 Vue监视数据的原理 [3 当要初始化一个空对象时,使用reactive最好先将里面的结构一起定义好;或者使用ref初始化一个空对象, <template> <div>{{ job.type }}</div> <div>{{ person.name }}</div> </template> <script> import { reactive } from 'vue' // 一定要引入reactive export default { steup() { let job = reactive({ // 先定义好reactive里面的结构 type: '', money: 0 }) let person = ref({}) function change(){ job.type = '农名工' // 对象类型的数据直接. job.money = 5000 arr = '更新数据1' // Vue3中可以实现数组的数据响应式了! } function newPerson() { name: '张三', age: 18 } return{ job, person } } } </script><span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue3学习之第二节:ref函数、reactive函数](https://blog.csdn.net/weixin_44209082/article/details/124367369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue(三)](https://blog.csdn.net/qq_53293179/article/details/123337834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue3系列教程——reactive](https://blog.csdn.net/ekcchina/article/details/125008795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值