How can I transfer data from an API response containing an array of objects to a new array in a Vue.js

How come I am getting NaN instead of "qwe" and "qweqweqwe" when trying to push the first 6 elements from an array of objects to a new array?

Imagine the array of objects retrieved from the API is structured like this:


0: {id: 340, name: "qwe", lastname: "qweqweqweq", …}
1: {id: 341, name: "qwe", lastname: "qweqweqweq", …}
2: {id: 342, name: "qwe", lastname: "qweqweqweq", …}
3: {id: 343, name: "qwe", lastname: "qweqweqweq", …}
4: {id: 344, name: "qwe", lastname: "qweqweqweq", …}
5: {id: 345, name: "qwe", lastname: "qweqweqweq", …}
6: {id: 346, name: "qwe", lastname: "qweqweqweq", …}
7: {id: 347, name: "qwe", lastname: "qweqweqweq", …}
8: {id: 348, name: "qwe", lastname: "qweqweqweq", …}

This is how my code is currently implemented:

.then(response => {
     console.log(typeof response.data, response.data);
     this.model = {
        name: response.data.name,
        lastname: response.data.lastname
    }
    this.userInfo.unshift(this.model.name + this.model.lastname); // The issue seems to be here
    if(this.userInfo.length > 6)
        this.userInfo.pop();
})
.catch(error => {
    console.log('An error occurred', error);
});
},

Answer №1

Let me clarify, you wish to transfer the first 6 elements from the response array to the userInfo array.

If this is your intention, you can achieve it by implementing the following snippet:

.then(response => {
    response.data.some((element, index) => {
          this.userInfo.push(element.name + element.lastname)
          return index > 5
    })
})

Answer №2

The reason for this issue is probably due to the fact that the object response.data does not have the properties name and lastname. When trying to add two undefined values together, it will result in NaN.

Answer №3

To shift only the initial six entries of the array items, you can employ this approach

const updatedArray = responseData.slice(0, 6); // The updatedArray will now contain the first six elements from the responseData.

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

Guide to Rolling a Set of 5 Dice

I am looking to develop a game involving 5 dice. I have already created a function to roll one die using a random method, but I am unsure how to extend this functionality to the remaining four dice without having to create a separate method for each one. ...

Even after implementing a setTimeout function, both Promises and Axios requests are still resulting in a

Recently, I've encountered an issue with the Reverse Geocode API from TomTom. The problem arises when I send multiple latitude and longitude coordinates (around 72 of them) to the API, resulting in frequent 429 responses. To address this issue, I att ...

Magento - when the page just can't take it anymore

I've encountered a problem with my Magento store. Half of the page is displayed and then it breaks. Here's the link to the page: When I check the page source, this is the code where it seems to break: <script type="text/javascript> ...

Launch the Image-Infused Modal

I am completely new to the world of Ionic development. Currently, I am working on a simple Ionic application that comprises a list of users with their respective usernames and images stored in an array. Typescript: users = [ { "name": "First ...

"Sticky Top Button That Stays in Place When Scrolling | Innovative CSS & jQuery

I've been inspired by the "Proceed to checkout" button on amazon.com and I want to recreate it. However, I'm facing a challenge as their website is not responsive and I can't find a way to view a device user agent in Chrome. To see what I&ap ...

Maintaining the "Date" value in React Native DatePickerIOS when returning from other pages

In my scenario, I am using the DatePickerIOS component. The example in the documentation initializes a new Date() and uses state to store and update the Date value. However, when navigating to another page and returning, the time changes and I find myself ...

The onclick function is malfunctioning within the Node.js environment

Having trouble with Node.js not working with JavaScript code This is my File structure: app.js index.html index.js However, when I run it without Node, it works and the alert shows up. app.js var http = require('http'); var fs = require(&apo ...

Changing a variable within a method does not automatically reflect in the child component

Need help with Vue update process for props/child components. Consider this component: <template> <v-card> <Modification v-model="newObject"></Modification> <OtherComponent @close="resetObject">& ...

Tips on conducting a statistical analysis without having to wait for the completion of an AJAX response

I am looking to track the number of clicks on a specific div with the id #counter and then redirect the user to a different website. To achieve this, I have implemented an ajax call on the click event of the #counter div. Upon successful completion of the ...

Error is being returned by the JSONP callback

Looking to grasp JSONP. Based on my online research, I've gathered that it involves invoking a function with a callback. Other than that, is the way data is handled and the data format similar to JSON? I'm experimenting with JSONP as shown below ...

Unable to add data to an Array once subscribed to BehaviorSubject

Hello everyone, this is my first time asking a question here. I hope it's clear enough for you to understand :) Let's dive straight into the issue at hand. I have a query regarding a behaviorSubject variable in TypeScript that is not allowing me ...

Does the gltf loader in three.js have compatibility issues with Internet Explorer 11?

Despite the claims on the official website that gltf files should load in three.js scenes using IE11, I have been experiencing issues with the loader. Even the examples provided by three.js fail to work on Internet Explorer when it comes to loading gltf fi ...

Encountering a 404 Not Found error while attempting to reach a Node/Express endpoint from the client side

I've developed a Node.js/Express REST API to be utilized by a frontend React application for an inventory management system intended for a garage sale. When attempting to add a new product, I'm trying to access the POST route http://localhost:300 ...

Archive a webpage with refreshed content using ajax

Is there a way to save a webpage that has been updated through an ajax request? I'm looking to preserve basecamp message threads, but exporting is not possible since I am not the account owner. When attempting to use the javascript command below: jav ...

Mastering Angular: Tackling a Directive with Two Parts

I'm working on a directive that's responsible for embedding footnotes into a body of text while providing popover functionality. The text-notes directive is designed to be placed on a parent element, loading content and its related text notes dyn ...

Iterating over an array while postponing a function

My goal is to create a continuous loop through an array of number values. These values will be used as delay parameters in a setInterval function, triggering another function each time. Here's what I've come up with: HTML: <p>On</p> ...

Using JavaScript, generate table rows and cells

Hey there, I'm struggling with formatting my table correctly within the designated section. Currently, it's printing the rows one below the other instead of creating a 'tr' and multiple 'td' elements for each input field. The ...

The transformation of all relative URLs into absolute URLs is carried out

The issue at hand seems to be peculiar to Chrome and Firefox, as IE does not exhibit the same behavior. In my angular application, I've noticed a strange phenomenon. Once a specific view is loaded, all XHR requests made using relative paths are autom ...

registration bootstrap form not functioning properly with ajax request

When the register function in a file named signup.php captures field values with Ajax and sends them to register.php, it should display a toast reply. // JavaScript code inside signup.php <script type="text/javascript"> function register(){ v ...

Aligning a DIV using javascript

Hey everyone, I'm encountering an issue with the JavaScript on my website. I'm struggling to center the div in order to properly display the image I click on. It seems to work fine on the second attempt, but on initial click, the div always appea ...