vue3+ts數組去重方及reactive/ref響應式顯示流程分析
vue3+ts數組去重方法-reactive/ref響應式顯示
簡單數組
數組使用reactive定義(推薦)
var arr: number[] = reactive([1, 2, 3, 4, 5, 6, 1, 2, 3]) // 變量都替換為arrX.ar var arrX = reactive({ ar: [1, 2, 3, 4, 5, 6, 1, 2, 3] })
數組使用ref定義
// 變量都替換為ar.value var ar = ref([1, 2, 3, 4, 5, 6, 1, 2, 3])
使用 Set 和 擴展運算符(…)將集合轉換回數組
const noRepeat1 = function () { arr = [...new Set(arr)]; console.log("...set " + newArr); }
...set 1,2,3,4,5,6
使用 Set 和 Array.from() 方法將集合轉換回數組
const noRepeat1 = function () { arr = Array.from(new Set(arr)); console.log("Array.from " + arr); }
Array.from 1,2,3,4,5,6
使用 filter 和 indexOf 進行判斷
const noRepeat2 = function () { // 檢查指定數組中符合條件的所有元素 arr = arr.filter(function (item, index) { return arr.indexOf(item) === index; // 因為indexOf 只能查找到第一個 }); console.log("filter-indexOf " + arr); }
filter-indexOf 1,2,3,4,5,6
使用 splice 和 indexOf 進行判斷
const noRepeat3 = function () { // 原地修改 for (let i = 0; i < arr.length; i++) { if (arr.indexOf(arr[i]) != i) { arr.splice(i, 1);//刪除數組元素后數組長度減1后面的元素前移 i--; //數組下標回退 } } console.log("原地 splice " + arr); }
原地 splice 1,2,3,4,5,6
使用 map 進行判斷,需要新建數組存儲
var newArr: number[] = reactive([]); const noRepeat4 = function () { newArr.length = 0; //如果newArr已經有值,需要置空 // 創(chuàng)建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素 let map = new Map(); arr.forEach((item) => { if (!map.has(item)) { map.set(item, true) newArr.push(item) } }) console.log("map " + newArr); }
map 1,2,3,4,5,6
使用 includes 進行判斷,需要新建數組存儲
var newArr: number[] = reactive([]); const noRepeat5 = function () { // 創(chuàng)建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素 arr.forEach((item) => { if (!newArr.includes(item)) { newArr.push(item) } }) console.log("includes " + newArr); }
includes 1,2,3,4,5,6
對象數組
使用reactive定義
// 給arrOb賦值,但reactive永遠是響應式的(推薦) const state = reactive({ arrOb: [ { key: '01', value: '樂樂' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' }, { key: '04', value: '哈哈' }, { key: '01', value: '樂樂' }, ], }); // 直接賦值操作可能會導致失去響應式的效果 const state2 = reactive([ { key: '01', value: '樂樂' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' }, { key: '04', value: '哈哈' }, { key: '01', value: '樂樂' }, ]);
使用冒泡排序的前后對比 和 splice,原地修改
const noRepeatObject0 = function () { for (let i = 0; i < state.arrOb.length - 1; i++) { for (let j = i + 1; j < state.arrOb.length; j++) { if (state.arrOb[i].key == state.arrOb[j].key) { state.arrOb.splice(j, 1); //因為數組長度減小1,所以直接 j++ 會漏掉一個元素,所以要 j-- j--; } } } console.log("挨個對比 - object list") console.log(state.arrOb) };
使用 object 判斷 key ,新建數組存儲
使用 object 對象訪問屬性的方法,判斷對象中是否存在key,新建數組存儲
interface Item { key: string; value: string; } var stateRes: Item[] = reactive([]) type stringKey = Record<string, boolean> const noRepeatObject1 = function () { var obj: stringKey = {}; for (let i = 0; i < state2.length; i++) { if (!obj[state2[i].key]) { stateRes.push(state2[i]); obj[state2[i].key] = true; } } console.log("obj - object list") console.log(stateRes) };
利用 reduce 方法遍歷數組,可原地修改
interface Item { key: string; value: string; } type stringKey2 = Record<string, number> const noRepeatObject2 = function () { var obj: stringKey2 = {}; state.arrOb = state.arrOb.reduce(function (item, next) { obj[next.key] ? '' : obj[next.key] = true && item.push(next); return item; }, [] as Item[]); console.log("reduce - object list") console.log(state.arrOb) };
利用 map 方法遍歷數組,可原地修改
const noRepeatObject3 = function () { let map = new Map(); for (let item of state2) { if (!map.has(item.key)) { map.set(item.key, item); } } // 數組定義:reactive([]); reactive({ arr: [] }); // xx.length = 0; // xx.push(...map.values()); // 數組定義:reactive({ arr: [] }); state4.arrOb = [...map.values()]; console.log("map - object list") console.log(state2) };
全部結果
關于響應式
const dataArray1 = reactive({ arr: [] }); // 創(chuàng)建一個響應式對象,其中包含一個空數組 const dataArray2 = reactive([]); // 創(chuàng)建一個響應式數組 dataArray1.arr = [1, 2, 3]; // 這樣賦值后,dataArray1.arr 仍然是響應式的,因為它仍然是 reactive 對象的一部分 dataArray2 = [1, 2, 3]; // 這樣賦值后,dataArray2 不再是響應式的,因為它已經被一個普通數組覆蓋了
創(chuàng)建一個響應式對象(如 dataArray1),并在其中包含一個數組,直接給這個數組賦值時,該數組仍然是響應式的,因為它是響應式對象的一部分。
創(chuàng)建一個響應式數組(如 dataArray2),并直接給它賦值一個普通數組時,它將不再是響應式的,因為它已經被一個普通數組覆蓋了。要保持響應式,需要在此數組基礎上修改,而不是用一個新數組覆蓋它。
失去響應式
let arr = reactive([]) function change(){ let newArr = [1,2,3] arr = newArr }
保持響應式
// 方法一:使用 ref let arr = ref([]) function change(){ let newArr = [1,2,3] arr.value = newArr } // 方法二:使用push 方法 let arr = reactive([]) function change(){ let newArr = [1,2,3] arr.push(...newArr) } // 方法三:外層嵌套一個對象 let arr = reactive({list:[]}) function change(){ let newArr = [1,2,3] arr.list = newArr }
到此這篇關于vue3+ts數組去重方法-reactive/ref響應式顯示的文章就介紹到這了,更多相關vue3 reactive/ref響應式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
element-plus一個vue3.xUI框架(element-ui的3.x 版初體驗)
這篇文章主要介紹了element-plus一個vue3.xUI框架(element-ui的3.x 版初體驗),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12vue3?使用defineAsyncComponent與component標簽實現動態(tài)渲染組件思路詳解
這篇文章主要介紹了vue3?使用defineAsyncComponent與component標簽實現動態(tài)渲染組件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03