I do not place a high importance on iterating within Callback functions

I'm currently juggling a hectic schedule and just starting to dig into callback functions. I'm facing an issue with parsing a JSON payload received from a webapp, specifically with handling an array of objects. In my script, I seem to only be retrieving the value of the last index in the array. Below is the code snippet along with the console output for reference. Any guidance on where I might be going wrong would be greatly appreciated.

var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
     var c1 = case1[i];
     c1.retrieveAttributes(function(){
            console.log(i+ " i");
             console.dir(c1.attributes);
     });
}

The value of 'i' in the console always prints as 6.

Answer №1

If you're encountering an issue with the 'closure' scope, consider passing 'i' into your function in this way:

var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
     var c1 = case1[i];
     c1.retrieveAttributes(function(i){
            console.log(i+ " i");
             console.dir(c1.attributes);
     });
}

This method should help maintain the accurate value of 'i'.

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

I encounter an error if an element is not displayed when using the isDisplayed() method

My situation Going to a specific page Should I click the element that is displayed If the element is not displayed, just ignore it My function exports.checkButton = function (driver) { driver.findElement(By.css(".btn.btn-danger.pull-right.remove- ...

The String retrieved from the API response does not support displaying line breaks, whereas a hard-coded string can successfully display line breaks

Greetings, My frontend is built on Angular 8, with a Java API service serving as the backend. I need to fetch a String from the backend, which will contain '\n' line breaks. For example: "Instructions:\n1. Key in 122<16 digit ...

What is causing the difficulty in accessing the 'query' feature within the API, and why is the question bank failing to display?

Just wanted to mention that I am still learning about class based components, setState, and other concepts in async JS like axios. Below is a very basic example of what I can currently do. This is App.js: import Questions from './components/Ques ...

Scala's FOLD method adeptly handles both valid and invalid code blocks alike

In my code snippet below, I am implementing JSON input validation using a controller. The validation process includes JSON schema validation and responses are provided based on whether the input is valid or invalid. However, I encountered an unexpected beh ...

Utilizing JSON to generate cxf-wadl2java mappings

After receiving a specification of a RESTful service in json format, I am tasked with creating a Java API library for the client. Although Swagger can handle this task effortlessly, my preference is to utilize the cxf-wadl2java Maven plugin. However, it s ...

What is the best way to populate dropdown menus using JavaScript?

I'm facing an issue with my ajax request where I am unable to populate the options of a select tag. This problem is occurring in multiple blocks where the select tag serves the purpose of choosing a type of product. Here is how my select tag looks li ...

Modifying the placeholder text in a Blueprint MultiSelect input field

Is there a way to change the default input text "Search..." in the MultiSelect component from Blueprint Labs? I checked the documentation and the example page, but couldn't find a way to customize the placeholder text. I thought it might be similar to ...

What is the best way to implement a subquery using EXISTS in Knex?

I'm currently facing challenges while constructing a query using knex, specifically when it comes to incorporating the 'WHERE' clause with the condition EXISTS (SELECT * FROM caregiver_patient WHERE patient_id IN (0,1)). Below is the origin ...

Populate a table dynamically in JavaScript using a variable

I have a table with IDs such as r0c1 = row 0 column 1. I attempted to populate it using the following script, but it doesn't seem to be functioning correctly: var data = new Array(); data[0] = new Array("999", "220", "440", "840", "1 300", "1 580", " ...

Using the contents of a text file as text in an HTML document

I've come up with a clever time-saving trick for my website template project. I want to streamline the process of changing large text files by having a separate plain text document that will automatically transfer its contents to a designated paragrap ...

Attempting to change JSON into a dictionary format using Python

I tried extracting JSON data from a website using the application/ld+json, but I am facing issues converting the string to a Python dictionary. When running the code in the terminal, I encountered an error stating JSONDecodeError("Expecting value", s, err. ...

"Troubleshooting: Resolving 404 Errors with JavaScript and CSS Files in a Vue.js and Node.js Application Deploy

I successfully developed a Vue.js + Node.js application on my local server. However, upon deploying it to a live server, I am facing a 404 error for the JavaScript and CSS files. I have double-checked that the files are indeed in the correct directories on ...

Exploring the dynamic duo of Django and DataTables: a guide on incorporating

Have you cautiously attempted to fetch data using AJAX, and displaying it in a datatable works seamlessly, but the issue arises when attempting to search or sort within the table. It seems that upon doing so, the data is lost, necessitating a page reload. ...

Update the table contents smoothly using the html helper PagedListPager without having to reload the entire

I am struggling with a specific line of code that utilizes the html helper's PagedListPager: @Html.PagedListPager(Model.kyc_paged_list, page => Url.Action("ClientDetails", new { id = ViewBag.ID, kyc_page = page, transaction_page = Model. ...

Server-side error in syncing Android SMS with JSON syntax

When attempting to synchronize messages from an Android device to a server using JavaScript Object Notation, I encountered a peculiar issue. While I was able to successfully sync all columns such as address, date, status, read, and type, the message body ...

Instructions for sending an image as a base64 string in JSON via HTTP POST on an Android device

Currently, I am facing an issue when trying to send a JSON request to the server using HttpPost. Below is the code that I am currently using: public static String makeRequest(String uri, String json) { HttpURLConnection urlConnection; Str ...

Alternative solution to avoid conflicts with variable names in JavaScript, besides using an iframe

I am currently using the Classy library for object-oriented programming in JavaScript. In my code, I have implemented a class that handles canvas operations on a specific DIV element. However, due to some difficulties in certain parts of the code, I had t ...

Transferring data from one HTML element to another using Angular [Ionic]

My project involves utilizing an ionic slide box to display a series of images within it. <ion-slide-box> <!-- I am looping through the images based on the image count --> <ion-slide ng-repeat="n in [].constructor(imageCount) track by $in ...

What is the correct way to change the v-model value of a child component within a parent component

Currently, I am in the process of mastering Vue.js and I have a specific goal. I want to modify the binding value of the child component's v-model and then trigger an event in the parent component. As I delve into the Element UI documentation, I aim ...

Creating Java objects from a REST API request to output JSON data

I recently incorporated spring rest templates into my Android project. As a result, I need to convert all of my JSON requests and responses into Java POJOs. Are there any tools available that can automatically generate these classes for me? I'm loo ...