Tips for adding a value to a specific object in an array

I am currently utilizing Vue along with Vuetify's v-data-table to display some data. Everything is functioning as expected, but I also need to incorporate data from another API. Therefore, I am looking for a way to add items to an array of objects.

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

Unfortunately, I am encountering an error stating that .push is not a function

Answer №1

Based on the code provided, it appears that you are attempting to push an item onto an object rather than an array, which is causing the error.

Additionally, it seems that your API response will return an array of objects. Instead of using .push, consider utilizing Destructuring assignment

For a live demonstration, refer to the following:

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() {
    // This is just a mock example. You can replace this 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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d2b28381d6f7325">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14626171607d726d54263a223a2526">[email protected]</a>/dist/vuetify.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c7a796978656a754c3e223a223d3e">[email protected]</a>/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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In Java, a character array can accept an entire string even if it is declared with a smaller size

As I delve into the world of Java string handling techniques, I stumbled upon an interesting discovery. Despite initializing my character array ch with a size of 0, it still successfully accepts and prints out the entire string from object s. This realiz ...

How can I prevent clearQueue() from removing future queues?

In my code, I have a button that triggers the showing of a div in 500ms when clicked. After the div is shown, a shake class is added to it after another 500ms. The shake class is then removed after 2 seconds using the delay function. However, if the user c ...

Identifying Three.js scenes by their names

After 6 hours of diving into Three.js, I am starting to understand new concepts. My current challenge involves accessing objects within a scene that is on a page with two canvases and scenes. Here is the HTML structure I am working with: <div id="my-c ...

Best practices for managing multiple checkboxes with identical v-model in vue.js

Could someone please assist me with the process of sending and retrieving multiple checkbox values from vue.js to the backend? I have been searching the internet for guidance on this topic, but with limited success. Here's the scenario: I have a cate ...

Ways to automatically run Java script again when new elements are included in the document model

Recently, I added paging and filtering to a page displaying a list of products. @model ProductFiltersViewModel ... <div class="row"> <aside class="col-3"> <nav class="sidebar card py-2 mb-4"> ...

Origin Access-Control-Allow Not Recognized

Currently, I am developing a node.js app and have encountered a strange issue: This particular Angularjs snippet... // delete hosts $scope.delete = function(row) { var rowData = { hostname: row.hostname, ipaddr: row.ipAddress, ...

Discover how to use jQuery to add CSS styles to every span element on a

I'm currently using JavaScript and jQuery to create a tree structure in ASP.NET MVC. There's an 'add' button that will add siblings and child nodes at the same level. To determine the appropriate action, I have implemented the followi ...

What is the process for deploying a next.js application with a custom express backend to a VPS or Heroku platform?

Does anyone have advice on deploying a next.js application with a custom express backend to either a VPS or Heroku? ...

Creating a dynamic shift in background color

Is it possible to use jQuery to slowly change the color of diagonal lines in a background styled with CSS, while also adding a fading effect? I have created a fiddle with the necessary CSS to display a static background. You can view it here: http://jsfid ...

Error with hyperlinks preventing redirection when clicking on the menu

$(document).ready(function () { $('.mobile-nav-button').on('click', function() { $( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(1)" ).toggleClass( "mobile-nav-button__line--1"); $( ".mobile-nav ...

What is the best way to transfer a file from Postman to a Node.js server using Multer?

Windows Express version 4.12.4 Multer version 1.0.1 Node version v0.10.22 I'm currently working on sending a file to my node.js server using Postman. I'm following the instructions provided in the readme here This is what I am sending wi ...

I encountered an error with no matching overload when attempting to make a call using the Tanstack query function

While I was successfully calling a single data in my database using the useEffect hook, now I am attempting to learn how to use tanstack@query. Unfortunately, I encountered an error when trying to call it. No overload matches this call. Overload 1 of 3, ...

The persistent state is not being saved correctly by Redux-Persist and is instead returning the initial

I have included redux-persist in my project, but for some reason it is not persisting the state as expected. Instead, I keep receiving the initial state whenever I reload the page. Below is the snippet of my root-reducer: import { combineReducers } from & ...

Sending slot-generated inputs from vue to the root vue instance

Can I transfer the value of input fields generated by a slot to the root vue instance? Consider my blade code: <template slot="update"> <td> <div> <input :value="name" type="text"/> </d ...

The HTML page is failing to call the function in the JavaScript application

I recently started taking a YouTube Javascript course on chapter 34 titled "Make start button work" Javascript tutorial My HTML, CSS, and Javascript files are all located in the same folder. I am using VS Code along with the Live Server extension for codi ...

Experiencing an issue with Angular 14 when trying to install apollo-angular as

For the past two days, I've been attempting to set up apollo on an Angular application. Unfortunately, I keep encountering the following error: The package <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8b9a85868685c78b8 ...

How can I break down an object with hyphenated key names?

Attempting to perform object destructuring on the following: { name: "Bryan", last-name: "Enid" } However, trying to destructure it like this is not successful: const {name, last-name} = req.body Is there an alternative method to destructure this ...

Leveraging a JavaScript variable within a PHP snippet

Similar Question: Sending a PHP string to a JavaScript variable with escaped newlines Retrieving a JavaScript variable from PHP I am trying to work with a Javascript function that accepts one variable, having some PHP code embedded within it. I am ...

Incorrectly managing user input

Currently, I am facing an issue with the user input handling in my program. The requirement is for the user to input a 3-digit number to be used later in a HotelRoom object constructor. However, due to restrictions set by my instructor, I cannot use string ...

The semantic-ui-react searchable dropdown feature may not show all of the options from the API right away

For some reason, when I attempt to call an API in order to populate the options for semantic UI, only a portion of the options are displayed initially. To view the full list, I have to first click outside the dropdown (blur it) and then click inside it aga ...