編寫Vue項目,如何給數(shù)組的第一位添加對象數(shù)據(jù)
更新時間:2022年04月21日 10:37:47 作者:郭寶
這篇文章主要介紹了編寫Vue項目,如何給數(shù)組的第一位添加對象數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Vue給數(shù)組第一位添加對象數(shù)據(jù)
核心代碼如下:
? ? ?this.menuBar.unshift({ ? ? ? ? ? title:"全部", ? ? ? ? ? id:13 ? ? ? ? })
全部代碼如下:
? ? data(){ ? ? ? ? return {? ? ? ? ? ? menuBar:[ ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"家具生活", ? ? ? ? ? ? ? id:14 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"攝影設計", ? ? ? ? ? ? ? id:15 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"明星美女", ? ? ? ? ? ? ? id:16 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"空間設計", ? ? ? ? ? ? ? id:17 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"戶型裝飾", ? ? ? ? ? ? ? id:18 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"廣告攝影", ? ? ? ? ? ? ? id:19 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"攝影學習", ? ? ? ? ? ? ? id:20 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"攝影器材", ? ? ? ? ? ? ? id:21 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"明星寫真", ? ? ? ? ? ? ? id:22 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"青春甜美", ? ? ? ? ? ? ? id:23 ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? title:"古典美女", ? ? ? ? ? ? ? id:24 ? ? ? ? ? ? }, ? ? ? ? ? ], ? ? ? ? ? } ? ? ? }, ? ? ? created(){? ? ? ? ? ? /*在數(shù)組的第一位添加對象數(shù)據(jù)*/ ? ? ? ? this.menuBar.unshift({ ? ? ? ? ? title:"全部", ? ? ? ? ? id:13 ? ? ? ? })? ? ? ? },
給數(shù)組添加新對象并賦值
方法一
適用于數(shù)組就只有一組
listData: [{name:"張三",age:18}], //直接添加對象 listData.sex="男"
方法二
適用于數(shù)組中有多組信息
listData:[], list:[{"張三","李四"}] //比如想把另外一個數(shù)組中編列出來的值加入到這個數(shù)組中 ? ?for (let index = 0; index < this.listlist.length; index++) { ? ? ? ? ? this.listData.push({ name: "" }); ? ? ? ? ? this.listData[index].name= this.list[index]; ? ? ? ? }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue 使用class創(chuàng)建和清除水印的示例代碼
這篇文章主要介紹了vue 使用class創(chuàng)建和清除水印的示例代碼,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12基于vue2.0+vuex的日期選擇組件功能實現(xiàn)
這篇文章主要介紹了 基于vue2.0+vuex的日期選擇組件功能實現(xiàn),詳細介紹了使用vue編寫的日期組件,,非常具有實用價值,需要的朋友可以參考下。2017-03-03Vue中computed(計算屬性)和watch(監(jiān)聽屬性)的用法及區(qū)別說明
這篇文章主要介紹了Vue中computed(計算屬性)和watch(監(jiān)聽屬性)的用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07