關於vue.js數組的變異方法
本篇文章跟大家分享了vue.js陣列的變異方法的相關內容,有興趣的朋友跟著學習參考下。
Vue 包含一組觀察陣列的變異方法,所以它們也會觸發視圖更新。這些方法如下:
push()
#pop()
shift()
unshift()
splice()
sort()
reverse()
#都有什麼功能?動手試驗了一下:
<body> <p id="app"> <p> push方法: <input type="text" v-model="text" @keyup.enter="methodByPush"> <input type="button" value="测试功能" @click="methodByPush"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> pop方法: <input type="button" value="测试功能" @click="methodByPop"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> shift方法: <input type="button" value="测试功能" @click="methodByShift"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> unshift方法: <input type="text" v-model="text" @keyup.enter="methodByUnshift"> <input type="button" value="测试功能" @click="methodByUnshift"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> splice方法: <input type="button" value="测试功能" @click="methodBySplice"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> sort方法: <input type="button" value="测试功能" @click="methodBySort"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> <p> reverse方法: <input type="button" value="测试功能" @click="methodByReverse"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </p> result显示的地方:<br> <span v-text="result"></span> </p>
<script> var vm = new Vue({ el: '#app', data: { items: [], text: '', result: '' }, methods: { methodByPush: function () { this.result = this.items.push(this.text) this.text = '' }, methodByPop: function () { this.result = '' this.result = this.items.pop() }, methodByShift: function () { this.result = '' this.result = this.items.shift() }, methodByUnshift: function () { this.result = '' this.result = this.items.unshift(this.text) this.text = '' }, methodBySplice: function () { this.result = '' this.result = this.items.splice(2,1,'yovan') }, methodBySort: function () { this.result = '' this.result = this.items.sort() }, methodByReverse: function () { this.result = '' this.result = this.items.reverse() alert(this.result) } } }) </script>
得到下面的結論:
後面想要在原始位置替換的值(可選)
# #reverse() 將陣列倒序,成功傳回倒序後的陣列
後來發現這些應該都是javascript本來的方法吧?以前javascript沒學好,剛好趁這次把這些方法的用法都拿回來!
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
關於vue偵測物件和陣列的變化分析VUEJS 2.0 子元件存取/呼叫父元件關於.vue檔案解析的實作以上是關於vue.js數組的變異方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用foreach循環移除PHP數組中重複元素的方法如下:遍歷數組,若元素已存在且當前位置不是第一個出現的位置,則刪除它。舉例而言,若資料庫查詢結果有重複記錄,可使用此方法移除,得到不含重複記錄的結果。

PHP中深度複製數組的方法包括:使用json_decode和json_encode進行JSON編碼和解碼。使用array_map和clone進行深度複製鍵和值的副本。使用serialize和unserialize進行序列化和反序列化。

PHP數組鍵值翻轉方法效能比較顯示:array_flip()函數在大型數組(超過100萬個元素)下比for迴圈效能更優,耗時更短。手動翻轉鍵值的for迴圈方法耗時相對較長。

多維數組排序可分為單列排序和嵌套排序。單列排序可使用array_multisort()函數依列排序;巢狀排序需要遞歸函數遍歷陣列並排序。實戰案例包括按產品名稱排序和按銷售量和價格複合排序。

PHP的array_group_by函數可依鍵或閉包函數將陣列中的元素分組,傳回關聯數組,其中鍵為組名,值是屬於該組的元素數組。

在PHP中執行陣列深度複製的最佳實踐是:使用json_decode(json_encode($arr))將陣列轉換為JSON字串,然後再轉換回陣列。使用unserialize(serialize($arr))將陣列序列化為字串,然後將其反序列化為新陣列。使用RecursiveIteratorIterator迭代器對多維數組進行遞歸遍歷。

PHP的array_group()函數可用來按指定鍵對陣列進行分組,以尋找重複元素。函數透過以下步驟運作:使用key_callback指定分組鍵。可選地使用value_callback確定分組值。對分組元素進行計數並識別重複項。因此,array_group()函數對於尋找和處理重複元素非常有用。

PHP陣列去重演算法的複雜度:array_unique():O(n)array_flip()+array_keys():O(n)foreach迴圈:O(n^2)
