簡體   English   中英

如何在 object 的特定數組中插入一個值

[英]How to insert a value inside a particular array of object

我正在使用 Vue ( vuetify ) v-data.table來顯示一些數據,它工作正常,同時我試圖顯示來自另一個 api 的一些數據,所以我需要知道如何將項目推入數組object。

axiosThis.tableDataFinal[i].push(axiosThis.printsumget[i])

我收到一個錯誤.push is not a function

根據您的代碼,您似乎正在嘗試對object而不是array執行推送操作。 這就是它給你那個錯誤的原因。

另外,根據我的理解。 您的 API 響應將為您提供一組對象。 因此,除了使用.push ,您還可以使用Destructuring assignment

現場演示

 new Vue({ el: '#app', vuetify: new Vuetify(), data () { return { headers: [ { text: 'Dessert (100g serving)', align: 'start', sortable: false, value: 'name', }, { text: 'Calories', value: 'calories' } ], desserts: [ { name: 'Frozen Yogurt', calories: 159 }, { name: 'Ice cream sandwich', calories: 237 } ] } }, mounted() { // Another axios api call. Just for a demo purpose I am using mock array here. You can replace it with an actual API response. const res = [{ name: 'Burger', calories: 200 }, { name: 'Chocolate Cake', calories: 250 }]; this.desserts = [...this.desserts, ...res]; } })
 <script src="https://unpkg.com/vue@2.x/dist/vue.js"></script> <script src="https://unpkg.com/vuetify@2.6.12/dist/vuetify.min.js"></script> <link rel="stylesheet" href="https://unpkg.com/vuetify@2.6.12/dist/vuetify.min.css"/> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"/> <div id="app"> <v-app id="inspire"> <v-data.table:headers="headers":items="desserts":items-per-page="5" class="elevation-1" ></v-data.table> </v-app> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM