国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Vue中遍歷數(shù)組的新方法實例詳解

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 21:53:28
文檔

Vue中遍歷數(shù)組的新方法實例詳解

Vue中遍歷數(shù)組的新方法實例詳解:1、foreach foreach循環(huán)對不能使用return來停止循環(huán) search(keyword){ var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return ne
推薦度:
導讀Vue中遍歷數(shù)組的新方法實例詳解:1、foreach foreach循環(huán)對不能使用return來停止循環(huán) search(keyword){ var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return ne

1、foreach

  foreach循環(huán)對不能使用return來停止循環(huán)

 search(keyword){
 var newList = []
 this.urls.forEach(item =>{
 if(item.name.indexOf(keyword) != -1){
 newList.push(item)
 }
 })
 return newList
 }

2、filter

  item對象就是遍歷數(shù)組中的一個元素,includes是es6中的新方法,在search方法中直接返回新數(shù)組

 search(keyword){
 return this.urls.filter(item =>{
 if(item.name.includes(keyword)){
 return item
 }
 })
 } 

3、findIndex

  返回true后index就可以獲取到匹配的元素在進行刪除

del(row){
 this.$confirm("確定要刪除嗎?", "刪除").then(action=>{
 var index = this.urls.findIndex(item =>{
 if(item.name == row.name){
 return true;
 }
 })
 this.urls.splice(index, 1)
});

4、some

  如果匹配成功就return true跳出some的循環(huán)

del(row){
 this.$confirm("確定要刪除嗎?", "刪除").then(action=>{
 this.urls.some((item, i) =>{
 if(item.name == row.name){
 this.urls.splice(i, 1)
 return true;
 }
 }) 
 });
}

5、上例子,在一個vue的data中存入一個固定的數(shù)組,對數(shù)組進行遍歷,實現(xiàn)搜索功能,刪除功能

  在el-table中 :data中綁定一個方法,方法中對固定的數(shù)組urls進行遍歷,返回一個新的數(shù)組實現(xiàn)搜索功能

<template>
 <div>
 <label style="float: left;">
 搜索關鍵字:
 <input type="text" class="form-control" v-model="keyword">
 </label>
 <el-table :data="search(keyword)" size="small" :stripe="true" :border="true" @select="select" @select-all="select">
 <el-table-column type="selection"></el-table-column>
 <el-table-column type="index"></el-table-column>
 <el-table-column label="網(wǎng)站名" prop="name" width="200">
 <template slot-scope="slot">
 <a href="slot.row.url" target="_blank">{{slot.row.name}}</a>
 </template>
 </el-table-column>
 <el-table-column label="網(wǎng)址" prop="url"></el-table-column>
 <el-table-column label="類型" prop="type" width="50"></el-table-column>
 <el-table-column label="國家" prop="country" width="50"></el-table-column>
 <el-table-column label="操作" width="50">
 <template slot-scope="slot">
 <el-button size="mini" type="text" icon="el-icon-delete" @click="del(slot.row)"></el-button>
 </template>
 </el-table-column>
 </el-table>
 <el-divider content-position="left">表格操作</el-divider>
 <el-button @click="batchDelete" type="danger" icon="el-icon-delete" size="small">批量刪除</el-button>
 </div>
</template>
<script>
 export default {
 data() {
 return {
 keyword:'',
 selections: [],
 urls: [{
 name: "新浪",
 url: "http://www.sina.com",
 type: "資訊",
 country: "中國"
 },
 {
 name: "騰訊",
 url: "http://www.tencent.com",
 type: "聊天",
 country: "中國"
 },
 {
 name: "谷歌",
 url: "http://www.google.com",
 type: "資訊",
 country: "美國"
 },
 {
 name: "韜睿",
 url: "http://www.51i-star.com",
 type: "教育",
 country: "中國"
 }
 ]
 };
 },
 methods: {
 del(row){
 this.$confirm("確定要刪除嗎?", "刪除").then(action=>{
 /* this.urls.some((item, i) =>{
 if(item.name == row.name){
 this.urls.splice(i, 1)
 return true;
 }
 }) */
 var index = this.urls.findIndex(item =>{
 if(item.name == row.name){
 return true;
 }
 })
 this.urls.splice(index, 1)
 });
 },
 select(selections, row) {
 this.selections = selections;
 },
 batchDelete() {
 this.$confirm("確定要刪除嗎?", "刪除")
 .then(action => {
 for (var i = this.urls.length - 1; i >= 0; i--) {
 for (var j = this.selections.length - 1; j >= 0; j--) {
 if (this.urls[i].name == this.selections[j].name) {
 this.urls.splice(i, 1);
 break;
 }
 }
 }
 })
 .catch(error => {
 alert(error);
 this.$message('刪除取消');
 });
 },
 search(keyword){
 /* var newList = []
 this.urls.forEach(item =>{
 if(item.name.indexOf(keyword) != -1){
 newList.push(item)
 }
 })
 return newList */
 return this.urls.filter(item =>{
 if(item.name.includes(keyword)){
 return item
 }
 })
 }
 }
 }
</script>
<style>
</style>

6、效果圖為

總結

以上所述是小編給大家介紹的Vue中遍歷數(shù)組的新方法實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Vue中遍歷數(shù)組的新方法實例詳解

Vue中遍歷數(shù)組的新方法實例詳解:1、foreach foreach循環(huán)對不能使用return來停止循環(huán) search(keyword){ var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return ne
推薦度:
標簽: VUE 例子 解析
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: aa级毛片| 日韩在线视频线视频免费网站 | 国产欧美综合精品一区二区 | 欧美一区二区在线 | 欧美精品色精品一区二区三区 | 手机在线观看国产精选免费 | 99国产精品一区二区 | 久国产精品视频 | 国产高清在线 | 亚洲国产成人久久综合碰 | 国产成人久久精品二区三区牛 | 亚洲第一区视频 | 国产资源在线播放 | 中国特黄毛片 | 一级一级特黄女人精品毛片视频 | 精品在线免费视频 | 免费视频精品一区二区 | 夜精品a一区二区三区 | 高清不卡一区 | 欧美日韩中文字幕在线 | 911久久| 国产日韩在线视频 | 在线一区观看 | 夜色毛片永久免费 | 婷婷爱爱 | 国产一区成人 | 国产一区二区精品久久凹凸 | 亚洲国产视频网站 | 天天色啪| 欧美高清亚洲欧美一区h | 国产成人无精品久久久久国语 | 欧美日韩精品在线视频 | 美女露胸动态无遮挡 | 欧美在线观看视频免费 | 久久免费国产精品一区二区 | 久久久精品久久久久久久久久久 | 操碰97 | 99久久精品免费国产一区二区三区 | 欧美在线免费观看视频 | 日韩1页| 国内精品久久久久影院不卡 |