Vue數(shù)組添加元素的三種實(shí)現(xiàn)方式
1、push() 結(jié)尾添加
數(shù)組.push(元素)
var node1 = ['111','222']
var new_node = node1.push('aaa')
此時(shí)數(shù)據(jù)為
node1: ['111','222','aaa']
2、unshift() 頭部添加
數(shù)組.unshift(元素)
var node1 = ['111','222']
var new_node = node1.unshift('aaa')
此時(shí)數(shù)據(jù)為
node1: ['aaa','111','222']
3、splice()
方法向/從數(shù)組指定位置添加/刪除項(xiàng)目,然后返回被刪除的項(xiàng)目。
| 參數(shù) | 描述 |
|---|---|
| index | 必需。整數(shù),規(guī)定添加/刪除項(xiàng)目的位置,使用負(fù)數(shù)可從數(shù)組結(jié)尾處規(guī)定位置。 |
| howmany | 必需。要?jiǎng)h除的項(xiàng)目數(shù)量。如果設(shè)置為 0,則不會(huì)刪除項(xiàng)目。 |
| item1, …, itemX | 可選。向數(shù)組添加的新項(xiàng)目。 |
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @returns An array containing the elements that were deleted.
*/
splice(start: number, deleteCount?: number): T[];
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @param items Elements to insert into the array in place of the deleted elements.
* @returns An array containing the elements that were deleted.
*/
splice(start: number, deleteCount: number, ...items: T[]): T[];
例如:
1、刪除:刪除(任意個(gè)數(shù))
- 參數(shù)1:開始的索引
- 參數(shù)2:刪除的長(zhǎng)度
var node = ['11','22','33','44'] var new_node = node.splice(1,2) //返回被刪除的數(shù)組元素 //new_node 輸出 ['22','33'] console.log(new_node ) //node輸出 ['11','44'] console.log(node)
2、添加(任意個(gè)數(shù)): 插入起始位置、0(要?jiǎng)h除的項(xiàng)數(shù))和要插入的項(xiàng)(不限)
var node = ['11','22','33','44'] //添加splice(start,0,newInfo)--返回值為空數(shù)組 var new_node = node.splice(1,0,'aa','bb') // new_node 輸出 [] console.log(new_node) // node 輸出 ['11', 'aa', 'bb', '22', '33', '44'] console.log(node)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)移除數(shù)組中特定元素的方法小結(jié)
- vue去除數(shù)組指定位置元素的幾種方法
- Vue中filter使用及根據(jù)id刪除數(shù)組元素方式
- vue項(xiàng)目中向數(shù)組添加元素的3種方式
- vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄
- vue.js如何刪除數(shù)組中指定索引的元素
- Vuejs從數(shù)組中刪除元素的示例代碼
- Vue判斷字符串(或數(shù)組)中是否包含某個(gè)元素的多種方法
- vue中實(shí)現(xiàn)監(jiān)聽數(shù)組內(nèi)部元素
相關(guān)文章
vue 函數(shù)調(diào)用加括號(hào)與不加括號(hào)的區(qū)別
這篇文章主要介紹了vue 函數(shù)調(diào)用加括號(hào)與不加括號(hào)的區(qū)別,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-10-10
使用Vue和ECharts創(chuàng)建交互式圖表的代碼示例
在現(xiàn)代 Web 應(yīng)用中,數(shù)據(jù)可視化是一個(gè)重要的組成部分,它不僅能夠幫助用戶更好地理解復(fù)雜的數(shù)據(jù),還能提升用戶體驗(yàn),本文給大家使用Vue和ECharts創(chuàng)建交互式圖表的示例,需要的朋友可以參考下2024-11-11
Vue實(shí)現(xiàn)點(diǎn)擊圖片放大顯示功能
這篇文章主要為大家詳細(xì)介紹了如何利用Vue實(shí)現(xiàn)點(diǎn)擊圖片放大顯示功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2023-03-03
利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件
最近遇到個(gè)功能要求,想要在全局中調(diào)用組件,而且要在某些js文件內(nèi)調(diào)用,所以這篇文章主要給大家介紹了關(guān)于如何利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件的相關(guān)資料,需要的朋友可以參考下2021-08-08
vue使用html2PDF實(shí)現(xiàn)將內(nèi)容導(dǎo)出為PDF
將 HTML 轉(zhuǎn)換為 PDF 進(jìn)行下載是一個(gè)比較常見的功能,這篇文章將通過(guò)html2PDF實(shí)現(xiàn)將頁(yè)面內(nèi)容導(dǎo)出為PDF,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

