Is it possible to create an index for an associative array based on a string in JavaScript?

Within my JavaScript code, I am working with an associative (two-dimensional) array (myObj[x][y]). Each row in this array contains a different number of elements denoted by 'n', where the first value signifies the amount 'n' as shown below:

{"amount"=>"n", "key0"=>"value0","key1"=>"value1"..."keyn"=>"valuen"}

During a loop iteration, I need to access these values using the format:

for (i=o; i=n-1;i++){
  current=myObj[row-x].key-y; //code to interact with current
}

I am trying to figure out how to dynamically build the index .key-y so that I can successfully access the values. Is there a way to achieve this by constructing a string such as

"myObj[row-x].key-"+ i.toString()}"
?

The given JSON data...

"[{\"object_type\":\"villa\",\"object_name\":\"Villa at Maries\",\"object_id\":\"1\",\"mainimg\":\"images\/maries\/IMG1.jpg\",\"object_descreption\":\"BLA BLA BLA.\",\"smallimg0\":\"images\/maries\/img1f.jpg\",\"smallimg1\":\"images\/maries\/img1a.jpg\",\"smallimg2\":\"images\/maries\/img1b.jpg\",\"smallimg3\":\"images\/maries\/img1c.jpg\",\"amencount\":4,\"amenitie0\":\"A\/C\",\"amenitie1\":\"Kitchen\",\"amenitie2\":\"Wi Fi\",\"amenitie3\":\"Parking\"},{\"object_type\":\"villa\",\"object_name\":\"Villa at Agia Marina\",\"object_id\":\"2\",\"mainimg\":\"images\\agiamarina\...

Appreciate all the help provided! I have now found the solution, which was simpler than I thought!

Answer №1

Yes, you can use the following code to iterate through all your elements:

var numOfRows;
for (row=0; row < myItems.length; row++) {
    numOfRows = myItems[row].quantity;
    for (item=0; item < myItems[row].length - 1; item++) {
        currentVal = myItems[row]["item" + item];
        //add your logic here to handle currentVal and numOfRows
    }
}

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

Determine the specific table entry that was clicked on

Currently, I am working on developing a dynamic table that includes a button in one of its columns, which triggers a bootstrap modal. I'm a bit unsure on how to determine which button was clicked and how to populate the table accordingly. Admittedly, ...

Unable to locate npm module called stream

For some reason, our tests have stopped running since yesterday. The error message reads: module stream not found Upon investigation, we discovered that 'stream' is available as a core node module: https://nodejs.org/api/stream.html#apicontent ...

I encountered a data discrepancy while attempting to link up with a weather API

This is my debut app venture utilizing node.js and express for the first time. The concept behind this basic application involves connecting to an external API to retrieve temperature data, while also allowing users to input their zip code and feelings whi ...

An array pointer that points to constant values

I am looking to create a global array of const values, like so: const int NUMBERS[NUMBERS_SIZE] = {2, 3, 5, 7, 11}; My question is whether there is any way for the array NUMBERS to be modified? Is there a way to ensure that NUMBERS points to a const mem ...

Oops! Looks like there's an issue with the type error: value.forEach is

I am working on creating an update form in Angular 6 using FormArray. Below is the code snippet I have in editfrom.TS : // Initialising FormArray valueIngrident = new FormArray([]); constructor(private brandService: BrandService, private PValueInfoSe ...

Maximize the efficiency of an integer array in C++

My data consists of a 2D array of int16_t values. int16_t my_array[37][73] = {{**DATA HERE**}} The range of these values spans from just above to just below the limit of int8_t, with some repetition. I am looking to optimize this lookup table by reducing ...

Ways to make video controls visible following the initial click on the video

I have a collection of videos on my website and I would like them to display with a poster (thumbnail image) initially. The video controls should only appear once the user clicks on the video for the first time. Therefore, when the page is loaded, the cont ...

How to delete an element from an array with UnderscoreJS

Here's a scenario: var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]; I'm looking to delete the element with id value of 3 from this array. Is there a method to achieve this without using splice? Perhap ...

I am encountering a problem with IE11 where I am unable to enter any text into my

When using Firefox, I can input text in the fields without any issues in the following code. However, this is not the case when using IE11. <li class="gridster-form" aria-labeledby="Gridster Layout Form" alt="Tile Display Input column Input Row ...

Angular JS Troubleshooting: Application Not Functioning Properly

I've been learning AngularJS on codeSchool and I ran into an issue with my simple hello world app. It was working fine at first but then stopped completely. I can't seem to find the bug, so any help would be appreciated. Here is the HTML code: & ...

Exploring AngularJS JSON Parsing Techniques (such as utilizing ng-repeat)

My approach to processing json data looks like this: $scope.activities = response.data; console.log($scope.activities.length); var list = []; for (var i = 0; i < $scope.activities.length; i++) { console.log($scope.activities[i].name); list.pus ...

Exploring the synergies between Angular Dragula and utilizing the $index property

Currently, I have implemented an ng-repeat of rows that can be rearranged using Angular Dragula. Despite successful drag-and-drop functionality, the $index in ng-repeat remains constant for each item even after reordering. The screenshot below depicts the ...

What is the method for implementing an 'AND' condition in Firebase or its equivalent?

I have a query requirement where I am looking to display only specific data by using an 'AND' statement or its equivalent. To better explain, I am referencing the example given in the Firebase Documentation. // Find all dinosaurs whose height is ...

I am currently working on creating a navigation bar for a webpage using the express framework and pug library. However, when I navigate to the demo page endpoint, the screen appears blank and nothing is displayed

//In the following JavaScript code, I am trying to implement basic routing navigation using express. However, when I try to insert HTML into the demo page, nothing appears on the browser screen. const path = require("path"); const app = ...

Setting the initial date value for an Angular Material md-datepicker to a specific date

When using Angular Material md-datepicker, by default the system's current date is set as today's date and highlighted in the calendar. However, I am looking for a way to manually set any given date as today's date instead. Please see this i ...

getting a null response when using the map feature - coding battles

Given an array filled with integers, my goal is to generate a new array containing the averages of each integer and its following number. I attempted to achieve this using the map function. var arr = [1,2,3,4]; arr.map(function(a, b){ return (a + b / ...

PubSub.js offers a solution for efficiently managing multiple subscriptions or dealing with multiple callbacks in a more streamlined manner

I am currently exploring the most effective approach for handling a specific scenario. My goal is to establish the following flow: 1.) Obtain configuration data from the server (asynchronously) 2.) Execute doStuff() once the configuration data is receive ...

On startup of the chrome app, read and load a JSON file into a variable

As I develop a chrome app, my goal is to store all configuration defaults in json file(s) alongside other assets. I am currently using AJAX requests to load them, but I'm wondering if there is a more efficient way to handle this. Is there perhaps an o ...

Disregard any labels when it comes to clickable areas for mouse events

Here is a simple example in JSFiddle where a text is displayed over an image inside a div element. Depending on where the cursor hovers, it can exhibit three different behaviors: pointing hand over the image, allowing text selection over the content of the ...

Unable to iterate over an array within a single file Vue component

I created a vue.js app utilizing single file components. In the data section, I initialized an empty array like this: data: function () { return { teamKeys: [] } }, Next, I populate this array by pushing values into it within one of my methods: ...