在我們使用VUE+Element 處理界面的時候,往往碰到需要利用JS集合處理的各種方法,如Filter、Map、reduce等方法,也可以涉及到一些物件屬性賦值等常規的處理或者遞回的處理方法,以前對于這些不是很在意,但往往真正使用的時候,需要了解清楚,否則很容易腦袋出現短路的情況,本篇隨筆列出一些在VUE+Element 前端開發中經常碰到的JS處理場景,供參考學習,
1、常規集合的filter、map、reduce處理方法
filter函式的主要用途是對陣列元素進行過濾,并回傳一個符合條件的元素的陣列
const nums = [10,20,30,111,222,333] let newNums=nums.filter(function(n){ return n<100 })
輸出:[10,20,30]
map函式是對陣列每個元素的映射操作,并回傳一個新陣列,原陣列不會改變將newNums中每個數字乘2
const nums = [10,20,30,111,222,333] let newNums=nums.map(function(n){ return n*2 })
輸出:[20,40,60,222,666]
reduce函式主要用于對陣列所有元素的匯總操作,如全部相加、相乘等
const nums = [10,20,30,111,222,333] let newNums=nums.reduce(function(preValue,n){ return PreValue+n },0)
輸出:726
有時候可以結合幾種處理方式一起,如下綜合案例所示,
const nums = [10,20,30,111,222,333] let newNums=nums.filter(function(n){ return n<100 }).map(function(n){ return n*2 }).reduce(function(preValue,n){ return preValue+n },0)
結果:120
另外還有一個陣列集合的find方法,和filter方法類似,
find()方法主要用來回傳陣列中符合條件的第一個元素(沒有的話,回傳undefined)
var Array = [1,2,3,4,5,6,7]; var result = Array.find(function(value){ return value > 5; //條件 }); console.log(result);//6 console.log(Array);//[1,2,3,4,5,6,7]
同樣我們也可以在vue里面,利用require.context的處理機制,遍歷檔案進行處理,也需要用到了filter,如下代碼所示,
下面代碼是我對某個檔案夾里面的檔案進行一個過濾處理操作
const req = require.context('vue-awesome/icons', true, /\.js$/)
const requireAll = requireContext => requireContext.keys()
const re = /\.\/(.*)\.js/
const vueAwesomeIcons = requireAll(req).filter((key) => { return key.indexOf('index.js') < 0 }).map(i => {
return i.match(re)[1]
})
export default vueAwesomeIcons
2、遞回處理
有時候,我們需要從一個JSON集合里面,由于集合是嵌套的,如children里面還有chilren集合,根據某個關鍵屬性進行查詢,這種處理方式就要用到遞回了,
例如我定義的一個選單集合里面,就是這樣一個嵌套的結構,需要根據名稱來獲得對應的物件的時候,就涉及到了一個遞回處理函式,
首先我們來看看選單的JSON集合,
// 此選單資料一般由服務器端回傳 export const asyncMenus = [ { id: '1', pid: '-1', text: '首頁', icon: 'dashboard', name: 'dashboard' }, { id: '2', pid: '-1', text: '產品資訊', icon: 'table', children: [ { id: '2-1', pid: '2', text: '產品展示', name: 'product-show', icon: 'table' }] }, { id: '3', pid: '-1', text: '雜項管理', icon: 'example', children: [ { id: '3-1', pid: '3', text: '圖示管理', name: 'icon', icon: 'example' }, { id: '3-3', pid: '3', text: '樹功能展示', name: 'tree', icon: 'tree' }, { id: '3-2', pid: '3', text: '二級選單2', icon: 'tree', children: [ { id: '3-2-2', pid: '3-2', text: '三級選單2', name: 'menu1-1', icon: 'form' } ] } ] } ]
如果我們需要根據ID來遍歷查詢,就是一個典型的遞回查詢處理,
// 根據選單id來獲取對應選單物件 FindMenuById(menuList, menuid) { for (var i = 0; i < menuList.length; i++) { var item = menuList[i]; if (item.id && item.id === menuid) { return item } else if (item.children) { var foundItem = this.FindMenuById(item.children, menuid) if (foundItem) { // 只有找到才回傳 return foundItem } } } }
這里值得注意的是,不能在遞回的時候,使用下面直接回傳
return this.FindMenuById(item.children, menuid)
而需要判斷是否有結果在進行回傳,否則嵌套遞回就可能回傳undefined型別
var foundItem = this.FindMenuById(item.children, menuid) if (foundItem) { // 只有找到才回傳 return foundItem }
3、forEach遍歷集合處理
在很多場合,我們也需要對集合進行一個forEach的遍歷處理,如下根據它的鍵值進行處理,注冊全域過濾器的處理操作
// 匯入全域過濾器 import * as filters from './filters' // 注冊全域過濾器 Object.keys(filters).forEach(key => { Vue.filter(key, filters[key]) })
或者我們在通過API方式獲取資料后,對集合進行處理的操作
// 獲取產品型別,用于系結字典等用途 GetProductType().then(data =https://www.cnblogs.com/wuhuacong/p/> { if (data) { this.treedata = https://www.cnblogs.com/wuhuacong/p/[];// 樹串列清空 data.forEach(item => { this.productTypes.set(item.id, item.name) this.typeList.push({ key: item.id, value: item.name }) var node = { id: item.id, label: item.name } this.treedata.push(node) }) // 獲取串列資訊 this.getlist() } });
又或者請求字典資料的時候,進行一個非空值的判斷處理,
// 使用字典型別,從服務器請求資料 GetDictData(this.typeName).then(data =https://www.cnblogs.com/wuhuacong/p/> { if (data) { data.forEach(item => { if (item && typeof (item.Value) !== 'undefined' && item.Value !== '') { that.dictItems.push(item) } }); } })
forEach()方法也是用于對陣列中的每一個元素執行一次回呼函式,但它沒有回傳值(或者說它的回傳值為undefined,即便我們在回呼函式中寫了return陳述句,回傳值依然為undefined)
注意: 如果forEach里有兩個引數,則第一個引數為該集合里的元素,第二個引數為集合的索引;
4、Object.assign賦值方法
在有些場合,我們需要把全新的集合,復制到另一個物件上,替換原來物件的屬性值,那么我們可以利用Object物件的assign方法,
如在編輯界面展示的時候,把請求到的物件屬性復制到表單物件上,
var param = { id: id } GetProductDetail(param).then(data => { Object.assign(this.editForm, data); })
或者查詢的時候,獲得查詢條件,進行部分替換
// 構造常規的分頁查詢條件 var param = { type: this.producttype === 'all' ? '' : this.producttype, pageindex: this.pageinfo.pageindex, pagesize: this.pageinfo.pagesize }; // 把SearchForm的條件加入到param里面,進行提交查詢 param.type = this.searchForm.ProductType // 轉換為對應屬性 Object.assign(param, this.searchForm);
5、slice() 方法
slice() 方法可從已有的陣列中回傳選定的元素,
語法如下所示,
arrayObject.slice(start,end)
如下案例所示,
let red = parseInt(color.slice(0, 2), 16) let green = parseInt(color.slice(2, 4), 16) let blue = parseInt(color.slice(4, 6), 16)
或者我們結合filter函式對圖示集合進行獲取部分處理
vueAwesomeIconsFiltered: function() { const that = this var list = that.vueAwesomeIcons.filter(item => { return item.indexOf(that.searchForm.label) >= 0 }) if (that.searchForm.pagesize > 0) { return list.slice(0, that.searchForm.pagesize) } else { return list; } }
為了方便讀者理解,我列出一下前面幾篇隨筆的連接,供參考:
循序漸進VUE+Element 前端應用開發(1)--- 開發環境的準備作業
循序漸進VUE+Element 前端應用開發(2)--- Vuex中的API、Store和View的使用
循序漸進VUE+Element 前端應用開發(3)--- 動態選單和路由的關聯處理
循序漸進VUE+Element 前端應用開發(4)--- 獲取后端資料及產品資訊頁面的處理
循序漸進VUE+Element 前端應用開發(5)--- 表格串列頁面的查詢,串列展示和欄位轉義處理
循序漸進VUE+Element 前端應用開發(6)--- 常規Element 界面組件的使用
循序漸進VUE+Element 前端應用開發(7)--- 介紹一些常規的JS處理函式
循序漸進VUE+Element 前端應用開發(8)--- 樹串列組件的使用
循序漸進VUE+Element 前端應用開發(9)--- 界面語言國際化的處理
循序漸進VUE+Element 前端應用開發(10)--- 基于vue-echarts處理各種圖表展示
循序漸進VUE+Element 前端應用開發(11)--- 圖示的維護和使用
循序漸進VUE+Element 前端應用開發(12)--- 整合ABP框架的前端登錄處理
循序漸進VUE+Element 前端應用開發(13)--- 前端API介面的封裝處理
循序漸進VUE+Element 前端應用開發(14)--- 根據ABP后端介面實作前端界面展示
循序漸進VUE+Element 前端應用開發(15)--- 用戶管理模塊的處理
循序漸進VUE+Element 前端應用開發(16)--- 組織機構和角色管理模塊的處理
循序漸進VUE+Element 前端應用開發(17)--- 選單管理
循序漸進VUE+Element 前端應用開發(18)--- 功能點管理及權限控制
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/73754.html
標籤:JavaScript
上一篇:avue表單資料請求
下一篇:斐波那契列數JS的三種實作
