I am having trouble with my Vue nested For loop as it is only returning the first value of the second array. What could be

I am currently utilizing a nested For loop to retrieve data from JSON and then returning a variable for Vue frontend access. Oddly enough, I am only able to retrieve values from the initial element of the nested array. Can anyone assist with this issue? It's quite perplexing.

***There is consistently only 1 job, hence why I am iterating through jobs[0] exclusively.

I have experimented with forEach, For of, For loops, and attempted to use map (although unsure about correct implementation).

At present, I can successfully return the Keys using Vue. Please keep in mind that this example demonstrates the accurate retrieval of JSON data, with the "Data" being passed to functions.

function getData(Data) {
    var jobs = Jobs;
    var i;
    for (i = 0; i < jobs.length; i++) {
        return items;
    }
}

try {
    Data.values = getData(Data);
}
catch (e) {
    logError(e);
}

The following displays the Vue:

<div v-for="value in values">{{ value.Key }}</div>

Despite my efforts, it seems ineffective and I'm uncertain as to why?

function getData(Data) {
    var jobs = Jobs;
    var i;
    for (i = 0; i < jobs.length; i++) {
        var items = jobs[0].Items;
        for (i = 0; i < items.length; i++) {
            return items[i].Values;
        }
    }
}

try {
    Data.values = getData(Data);
}
catch (e) {
    logError(e);
}

The corresponding Vue segment follows:

<div v-for="value in values">{{ value.Key }}</div>

You'll notice that I've delved one layer deeper into the arrays. However, on the frontend, I still only extract the Key from the initial item. Though I can precisely specify the item to access using return items[2].Values, indicating that I can indeed reach them individually. The JSON formatting is accurate, and I've utilized For loops similarly numerous times without Vue (I'm relatively new to Vue) for data extraction. Before suggesting a Vue-specific method or component, please grasp the necessity for me to utilize separate JavaScript files and functions like this one to return the values accessible on the frontend. It might be an unconventional setup, but it's what I have to work with. I'm somewhat disoriented overall, but resolving this dilemma would be truly beneficial because presently, I would resort to employing the initial example followed by something akin to this (again just illustrative):

<template v-for="item in items">
<div v-for="value in values">{{ thing.Key }}</div>
</template>

This approach suffices for simple tasks, but as I delve deeper into the JSON structure, complications arise. Some of the values are buried 5 or 6 levels deep, encompassed by conditional statements, necessitating mathematical calculations on certain values to parse strings and tally other elements. Moreover, some items are objects while others at the same hierarchical level in the JSON are arrays. It's quite convoluted, and refining it within the for statements would prove immensely beneficial.

All forms of assistance are sincerely appreciated.

Answer №1

When working with the array Jobs, make sure you only declare the variable i once inside the loop. If you are iterating through a single array item for Jobs, there's no need to loop through jobs and then access its items.

Additionally, by returning a value within the loop, it will prevent the loop from executing past the first iteration.

function getBarCodes(Jobs) {
    var arr = [];
    var items = Jobs[0].Items;
    for (var i = 0; i < items.length; i++) {
        arr.push(items[i].Values)
    }
    return arr;
}

A more efficient approach would be to iterate directly through the items, gather the Values for each item by adding them to the collection array, and finally return the array.

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

Iterating through an instance of stdClass named "object1"

object(stdClass)#1 (1) { ["trackingNo"]=> array(18) { [0]=> string(15) " 888005324912 " [1]=> string(16) " 1900530244582 " [2]=> string(15) " 778180519352 " [3]=> string(16) " 1000237325384 " Is there ...

Navigating Angular QueryList through loops

I am currently trying to gather all the images in my component and store them in an array. To achieve this, I am utilizing Angular's @ViewChildren which returns a QueryList of ElementRef: @ViewChildren('img', { read: ElementRef }) images: Q ...

Display a "Loading" image in the gallery before anything else loads

Can a animated loading gif be implemented to load before the gallery images, or would it serve no purpose? The image will be loaded as a background using CSS. <link rel="stylesheet" href="loading.gif" /> Appreciate your help! ...

jQuery AJAX Triggered Only Once in Callback Function

I am facing an issue with the jQuery get function within my updateMyApps. The function works fine when called directly after it is declared. It successfully loops through the data and appends elements to the DOM. However, when I submit the form #addapplic ...

What is the best way to reset the column filter cell in Material-Table?

Within my Material-Table, I am utilizing this unique TextField to craft a specialized filter cell for each column. My goal is to employ the endAdornment as a button that will reset the filter, but I am struggling with removing the current value. filterCom ...

A step-by-step guide to making a pie chart using Vue Echarts

Currently, I am delving into VueJS and using the Vue Echarts library to craft engaging data visualizations. Take a look at this demonstration showcasing a Polar chart with Vue Echarts: Official Website: GitHub Page: https://github.com/ecomfe/vue-echarts ...

jQuery - Wait for the completion of one UI change before initiating another UI change

Is there a way to implement the functionality where $("#login").toggle("drop", {direction: "left"}); is executed first and upon completion, $("#register").toggle("drop", {direction: "right"}); is then carried out? The issue arises from the fact that the ...

Remove console.log and alert statements from minified files using uglifyjs-folder

Currently, I am minifying multiple files in a directory using the uglifyjs-folder feature within my npm configuration in the package.json file as shown below: "uglifyjs": "uglifyjs-folder js -eyo build/js" The process is effectively minifying all the fil ...

In Angular 5, what is the best way to transform an array into an object and cycle through a list of

JSON: The JSON I have contains a list of userids that I want to iterate through, but I'm having trouble fetching the content in a list format. { "data": { "items": [ { "regions": "India", "owner ...

Effortlessly refresh a data object in Vue.js without relying on a function

I need assistance with the following: <Checkbox label="View" :initialState="data.something" @updateStatus="updateCheckbox" > </Checkbox> The variable data.something is a b ...

Tips on formatting vueJS HTML5 with tidy without compromising its structure or content

Using the tidy tool on this VueJS code causes it to completely break: Here is the initial VueJS HTML code: $ cat sample_vue.html ... <tbody> <tr v-for="task in tasks"> <td><strong>{{task.title}}< ...

What is the process for displaying the following text after deleting the particle text?

Recently diving into the world of vanilla JS and WebGL content, I managed to create a particle text effect that distorts on mouse hover. These particles are interconnected with lines, as shown in the code snippet below. const canvas = document.getElementBy ...

Guide on linking Influxdb information in a Vue application using node.js?

I have successfully connected my InfluxDB database to my Vue app and can log data in the terminal using the code below: // index.js import express from "express"; // These lines make "require" available import { createRequire ...

Loading files using $scriptjs or any other asynchronous loader allows for seamless integration of external resources

Imagine I have developed an AngularJS application that lazy loads controller files (using $scriptjs) and all dependencies when the user navigates to a specific route. This application consists of 3 routes: A, B, and C. My question is: if the user navigate ...

Advancement in the processing time of a HTTP response that is significantly prolonged

I am seeking a way to return an array of JSON objects (requested by an AJAX call in an AngularJS web application) that represent specific files on the server implemented with node.js. Typically, the result is quickly accessed from a database. However, if ...

I am unfamiliar with this specific JavaScript algorithm problem from Codewars

I need help with a JavaScript algorithm question This problem involves extracting two letters from the middle of odd-numbered characters My confusion lies in function getMiddle(s) { //Code goes here! let answer = ""; if (s.length % 2 !== 0) { a ...

Tips for executing a function in the HC-Sticky plugin?

Currently, I am utilizing the HC-Sticky JavaScript plugin and endeavoring to utilize the documented reinit method. However, I am facing difficulty in understanding how to execute it. In this CodePen demo, a basic setup is displayed along with an attempt t ...

What is the best method for extracting a value from a JSON file within an array using electron and node.js?

Hey there, I'm currently working with a JSON file that contains an array of value sets. I'm trying to figure out how to extract the first value from the initial set, specifically the value 3 under the key "pink". This is all being done in Node.js ...

Check for compatibility of overflow:scroll with mobile browsers

Is there an easy JavaScript method that works across different devices and libraries? I am looking to assign a class to the html element in order to enable scrollable containers on mobile devices when needed. I want to follow a similar approach to Modern ...

Insert text into the cursor location within Summernote using JQuery

Currently, I am working on a flutter application where I have implemented the summernote editor using JQuery. ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain); String txtIsi = data.text .replaceAll("'", '\&bsol ...