How can I increase the value of a variable that is displayed within a loop using JavaScript?

I'm struggling to use arrays for this task, even though it should be done that way. When I receive a set of variables through JSON, they look something like this:

--variables I don't need... and then:
"s_title_a": "---",
"s_title_b": "---",
"s_title_c": "---",
 .........

My challenge is to print all of them using a loop that utilizes an incrementing character as a counter, with the counter serving as the suffix of the variable name. Here's what I attempted:

   function charLoop(from, to, callback)
        {
            var i = from.charCodeAt(0);
            var to = to.charCodeAt(0);
            for(;i<=to;i++) callback(String.fromCharCode(i));
        }


        charLoop("a", "l", function(char) {
            console.log( window["articolo.s_title_" + char] );
        });

Unfortunately, this solution doesn't seem to work as expected. All I see is:

undefineda
undefinedb
undefinedc
and so on...

It appears that the suffix is being added to the variable value instead of the variable name before fetching its value. I would appreciate any help. Thank you.

Answer №1

To retrieve the desired information within the callback function, simply utilize the code snippet below:

console.log( articolo["s_title_" + char] );

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 there a way to use a POST request and Mongoose in Express to add a new object?

I am struggling to figure out how to use .Router() for creating a POST request route. I have only worked with getById before. Can someone help me create a route for POST requests? ./generalRepository.js function Repository() {} Repository.prototype.getB ...

Utilizing Jquery for Enhancing Annotations

I am in the process of developing a website for essay writing tests. I have implemented a feature where users can input their essays using a text area, and now I want to be able to make comments on that text similar to PDF annotations or highlighting. I at ...

What are the steps to fix the eslint error related to "Generic Object Injection Sink"?

I'm attempting to access a JSON array, but every time I try to read the array/value by passing the JSON object key like this- json[key] An Eslint error occurs- [eslint] Generic Object Injection Sink (security/detect-object-injection) I understand ...

Triggering specialized event

Utilizing a custom Yeoman generator programmatically within my Node.js module has posed some challenges. I have created an Adapter to replace the default TerminalAdapter. The issues at hand are: When triggering a custom event using the emit method, ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...

How can you place text on top of an image using JQuery and CSS?

I've been working on overlaying text over an image using Jquery and css. The overlay text is displaying nicely on the image, but I'm struggling to change the font and size of the text. I attempted to make changes in the CSS for the font family an ...

"Vue's scope is always kept fresh and updated

Recently I've been working with a combination of vue.js and jquery. In my current scenario, I attempted to insert sidebar html code (containing vue syntax) into main.html using jquery. Unfortunately, upon injecting sidebar.html into main.html, vue.js ...

react/axios -- encountering issues retrieving data through API request

This is my first time working with React, and I am attempting to make an API call in my code. I found an example online that worked perfectly, so I decided to follow the same approach for my own API call. The call was successful, but when I tried to print ...

Creating interactive tabs within the HTML document with AngularJS

Trying to customize the tab layout on my HTML page and I have a code similar to this: <form name="requestForm"> <div class="form-group col-md-6 md-padding"> <div class="text-primary"> <h3>Create Request</h3> ...

Transferring an Object from one AngularJS Controller to Another

Currently delving into the world of AngularJS, I've encountered a seemingly trivial problem with no solution in sight. My issue lies in managing two lists/controllers created by a factory service. Specifically, I'm facing difficulties removing ...

Discover the complete file path using node.js incorporating the express-busboy module

I am currently utilizing node.js and express-busboy for the purpose of uploading a file via a file input form to my server. Once uploaded, the file will be stored in a directory with a path resembling something like root/useruploaded/formattached/somerando ...

In JavaScript, whenever there is an error for each variable A in B, it means that B is an array of arrays

I stumbled upon this code snippet: var B = []; var data = []; data.push("string"); // .... B.push(data); // ... for each (var A in B){ console.log(B); console.log(A); let obj = A[0].split("|", 3); console.log(obj[0]); ...

Creating a CSS style that automatically adjusts the font size within a container that spans the full height of the

Issue: I have a container set to 100vh, but as the screen size changes, I want the font size to adjust accordingly and always stay within the container without overflowing. Thoughts: While I understand this can be achieved easily with @media CSS rules, I& ...

The field 'shouldComponentUpdate' cannot be reassigned to itself

I encountered a TypeScript error while using shouldComponentUpdate: The error message states: "Property 'shouldComponentUpdate' in type 'Hello' is not assignable to the same property in base type Component<IProps, any, any>." ...

Why is it that this code can upload photos successfully, but not text files?

This is my PHP file for handling form actions. I have successfully implemented image uploads, but I am encountering an 'Invalid File Error' when trying to upload text files. What could be causing this error and how can I resolve it? <?php e ...

Ways to emphasize a specific row within a table for special occasions

I have limited knowledge of CSS and JavaScript, but I am looking for a way to create a notification highlight that functions similarly to when someone comments on a Facebook post. When clicked, it should direct me to the specific comment with a temporary h ...

At what point should you invoke db.close() while utilizing cursor.forEach()?

When working with .toArray(), it is common practice to include db.close() within the callback function. For example: db.collection('grades').find(query).toArray(function(err, docs) { if (err) throw err; console.dir(docs); db.close(); }); ...

Determine if a file input is linked in React.js Material UI

Currently, I am working with a Material UI <TextField /> component that has a type=file prop to allow file attachments. My goal is to trigger an event when a file is attached to this TextField. However, my search results only provided JQuery soluti ...

Maximizing the potential of JavaScript by utilizing effective tags

Looking to enhance my JavaScript code by adding a maximum tags option limited to 6 with no duplicates. Check out the current snippet below: <div id="tags"> <span>php</span> <span>c++< ...

IE7 is throwing an error saying "Object Expected" when handling the JSON response. This issue does not

Just when I thought I was ready to launch my webapp, IE7 decides to throw a wrench in my plans! I am using the JQuery Form plugin for uploading data to my server. Everything works perfectly on Chrome and Firefox, but IE7 is giving me an "Object Expected" ...