The array's length function is unexpectedly showing zero despite containing elements

The quantity of elements in this code is showing as 0 even though it contains a value.

Sample snippet:

for (var q = 0; q < data.data.length; q++) {
var test1 = {
    imgId: data.data.imgId,
    imgString: data.data.imgString
};
vm.imgArr.push(test1);}

console.log(vm.imgArr.length); // displays zero

Example dataset:

data.data = [
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
},
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
},
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
}];

Answer №1

You have mistakenly accessed the data within the for loop. To retrieve the value, use data.data[q].imgId

Consider replacing your current for loop with this:

for (var q = 0; q < data.data.length; q++) {
    var test1 = {
        imgId: data.data[q].imgId,
        imgString: data.data[q].imgString
    };
    vm.imgArr.push(test1);
}

Review the following code snippet:

var data={};

data.data = [
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
},
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
},
{
    "imgId": "asdasd21321312312asdad",
    "imgString": "heasjahdasdjashdjashd"
}];

var vm={imgArr:[]};
for (var q = 0; q < data.data.length; q++) {
    var test1 = {
        imgId: data.data[q].imgId,
        imgString: data.data[q].imgString
    };
    vm.imgArr.push(test1);
}

console.log(vm.imgArr);

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

Is it possible to modify the sizes parameter of the GPUComputationRenderer?

Currently, I am utilizing gpuCompute = new GPUComputationRenderer( sizeX, sizeY, renderer ); for texture purposes. I am looking to update the values of sizeX and sizeY within this code snippet. However, after searching through the library, I have not been ...

Which is better for testing in Cypress: classes or functions?

When it comes to testing in Cypress, which approach do you believe is more efficient? Functions: 'support/pages/login.js' export const login = (username, password) => { cy.get('#username').type(username); cy.get(& ...

JavaScript function not altering variable

I recently encountered a problem with my code where I noticed that a variable's value was changing within an if statement inside an SQL query function, but the change was not reflected globally, only locally. Here is the snippet of code in question: ...

Tips for creating a helper function that returns a promise for a React hook function

So, I have this React functional component that makes use of both the useState and useEffect hooks: import React, { useState, useEffect } from 'react'; import { requestInfo } from './helpers/helpers'; const App = () => { const [pe ...

Struggling to iterate through the children of a variable with the help of express-handlebars?

Currently, I am in the process of developing an application using Javascript along with express, express-handlebars, and MySQL. One of my main tasks is to establish a route that follows the pattern '/viewowner/:ID/scoreboards/:year' where ID sig ...

Angular threw an error expecting an assignment or function call, but encountered an expression instead

I have integrated [JSLint][1] into my Angular App, using ES6 style IIFE's. However, I am encountering an error message: Instead of seeing an expression, it was expected to find an assignment or function call This issue arises when analyzing the fo ...

Trigger a Vue method using jQuery when a click event occurs

I'm attempting to attach a click event to an existing DOM element. <div class="logMe" data-log-id="{{ data.log }}"></div> ... <div id="events"></div> Struggling to access external Vue methods within my jQuery click handler. A ...

Guide to changing the background colors of multiple elements when hovered over by the mouse?

I want to customize my website's search bar by changing the background color when it is hovered over. Currently, the search bar has two main elements: the text box and the submit button. I have successfully programmed the text box element to change to ...

Retrieving a specific value from a PHP array

I'm working with a PHP array that has been filtered based on the datasetID, resulting in a single record stored in $result. My goal is to output a specific attribute, such as ["genus"]. I attempted to do this using echo $result[0]['gen ...

Implementing endless scrolling in Django by utilizing Django Paginate to continuously display results from the repeated

Introduction: I've utilized the following link to implement infinite scroll in my project: Here is the Github repository for that link: https://github.com/sibtc/simple-infinite-scroll The link provides instructions on adding infinite scroll function ...

JQuery is facing issues with its .each loop when trying to iterate through elements that have dynamically changing class names

When attempting to use variables to search for elements with the same class name as the variable's value within the .each loop below, no alert is triggered. Why is this happening? var NameString = $("#StoreInputs").val(); var NameArray = NameString.s ...

The error in ReactJS is coming from the render method of the `News` component

While working with React JS, I encountered an error when trying to run a particular code snippet. The error message displayed was: "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got ...

The error message "Potree-Core: THREE is undefined" indicates that the

I'm currently developing a web-based application that can be used offline by other users on their local machines. Within my NPM project, I am utilizing Potree-Core, which relies on THREE.js. However, the source code does not include an import for THR ...

What strategies should be employed to manage data-driven input in this particular scenario?

In the process of creating a small webpage, I have designed 2 navigation panels - one on the left and one on the right. The leftNav panel contains a list of different flower names. While the rightNav panel displays images corresponding to each flower. A ...

A Vue component nested within another Vue component

Hey there! Check out this snippet of HTML code: // index.html <div data-init="component-one"> <...> <div data-init="component-two"> <button @click="doSomething($event)"> </div> </div> This part basically invol ...

What is the best way to apply a rotation of +/- x degrees to a shape in Python using numpy arrays?

I am a beginner in Python and I'm attempting to rotate a shape, specifically a closed circle with cutouts inside, by a user-defined number of degrees. The rotation has to include the cut outs, so I'm looking to accomplish this using numpy arrays. ...

Display and conceal a div using jQuery upon hovering, and maintain its visibility until the mouse moves out

I am having an issue with my language drop down menu. When I hover over the flag, the language options should display and stay visible. However, when I move the mouse away from the language options or hover over the flag again, the language options should ...

When attempting to log API results in Next.js, a TypeError occurs because the property is undefined

I'm encountering a rather unusual error. In my Next.js app, I am attempting to console log the results of an API call to see what data I receive. The API function is responsible for verifying if a user is logged in, and if they are, I render a specifi ...

Angular attempts to fetch templates via ajax while utilizing the $templateCache

Currently, I am utilizing gulp (html2js) to compile all of my html into Angular's $templateCache and then using browserify to include the desired files. However, being new to angular, it seems odd to me that even when I utilize $templateCache, Angula ...

Hovering over a Raphael animation - What's preventing it from functioning?

I'm currently working on a pie chart using Raphael, and I want to add a tooltip that displays text when hovering over a specific segment of the chart. The hover event is functioning correctly, but I'm having trouble adjusting the tooltip text coo ...