简体   繁体   English

如何在 object 的特定数组中插入一个值

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

I am using Vue ( vuetify ) v-data.table to show some data, Its working fine, in the same time I am trying to show some data from another api, So I need to know how to push a items inside of a array of object.我正在使用 Vue ( vuetify ) v-data.table来显示一些数据,它工作正常,同时我试图显示来自另一个 api 的一些数据,所以我需要知道如何将项目推入数组object。

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

I am getting an error .push is not a function我收到一个错误.push is not a function

As per your code, Looks like you are trying to perform a push operation on an object instead of an array .根据您的代码,您似乎正在尝试对object而不是array执行推送操作。 That's the reason it is giving you that error.这就是它给你那个错误的原因。

Also, As per my understanding.另外,根据我的理解。 Your API response will give you an array of objects.您的 API 响应将为您提供一组对象。 So instead of using .push , You can use Destructuring assignment因此,除了使用.push ,您还可以使用Destructuring assignment

Live Demo :现场演示

 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