欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vuejs從數(shù)組中刪除元素的示例代碼

 更新時間:2023年02月01日 16:06:22   作者:ZNineSun  
這篇文章主要介紹了Vuejs從數(shù)組中刪除元素的示例代碼,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Vuejs從數(shù)組中刪除元素

使用方法:arr.splice(arr.indexOf(ele),length):表示先獲取這個數(shù)組中這個元素的下標(biāo),然后從這個下標(biāo)開始計算,刪除長度為length的元素 這種刪除方式適用于任何js數(shù)組

eg:

<template>
 <div class="users">
	<button type="button" class="btn btn-danger" v-on:click="deleteUser(user)"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>刪除</button>	
 </div>
</template>

<script>
//引入jquery

export default {

  data(){
		return {
			
			users:[
				{
					name:'zx',
					age:18,
					addrress:'江蘇南京',
					email:'1773203101@qq.com',
					contacted:false,
				},
				{
					name:'zhiyi',
					age:19,
					addrress:'中國北京',
					email:'1773203101@qq.com',
					contacted:false,
				},
				{
					name:'zhuxu',
					age:20,
					addrress:'中國上海',
					email:'1773203101@qq.com',
					contacted:false,
				},
			]
		}
	},
	methods:{
		deleteUser:function(user){
			//表示先獲取這個元素的下標(biāo),然后從這個下標(biāo)開始計算,刪除長度為1的元素
			this.users.splice(this.users.indexOf(user),1);
		}
	}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--scope只會影響到當(dāng)前組件的樣式-->
<style scoped>
</style>

補充:vue 刪除數(shù)組中的某一條數(shù)據(jù)

一、刪除普通數(shù)組

let arr = [1,2,3,4,5];

//方法一
let index = arr.indexOf('3');
arr.splice(index, 1)
//打印結(jié)果 [1,2,4,5]
//方法二
let index = arr .findIndex(item => {
            if (item == '3') {
              return true
            }
          })
arr.splice(index, 1)
//打印結(jié)果 [1,2,4,5]

二、刪除數(shù)組對象

let arr = [
    {
       id:1,
       name:'張三'
    },
    {
       id:2,
       name:'李四'
    },
    {
       id:3,
       name:'王二'
    },
    {
       id:4,
       name:'麻子'
    },
];
let id1 = arr.findIndex(item => {
      if (item.id == '3') {
          return true
      }
 })
 arr.splice(id1, 1)

三、歡迎添加更多方法,以上是我覺得最簡單的方法

到此這篇關(guān)于Vuejs從數(shù)組中刪除元素的文章就介紹到這了,更多相關(guān)vue數(shù)組中刪除元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論