Exploring the World of Dynamic Arrays and Objects in JavaScript

Can we achieve this?

I am looking to create an array with a dynamic name and content that can be easily expanded and accessed.

variable = {};
var values = ['item', 'item2'];
for(key in values){
    variable[values[key]] = [];
}

If it's possible but not through the above method, then how can it be done?

Answer №1

It is entirely achievable, just ensure that the property belongs to the object and is not inherited from a higher level in the prototype chain:

customObject = {};
var elements = ['item', 'item2'];

Using for..in loop:</p>

<pre><code>for(var index in elements){
    if(elements.hasOwnProperty(index))
       customObject[elements[index]] = [];
}

Using regular for loop:

for(var j = 0; j < elements.length; j++)
   customObject[elements[j]] = [];

Answer №2

let newObj = {};
let items  = 'item item2'.split(' ');
for (let x=0,length=items.length;x<length;++x){
  newObj[items[x]] = [];
}

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 Arrays.asList() not functioning correctly?

I have an array of floats and I would like to convert them into a list with the same elements. Instead of adding each element individually, I attempted to use the Arrays.asList method. However, I encountered an issue. The following code works: List<Int ...

Enable retrieval of calculated time duration in jQuery time picker

After implementing two separate timepickers for calculating a time duration, I am looking to access this computed duration for further calculations later on the page. How can I retrieve the duration that is already displayed to the user after using the sec ...

Is it possible to interact with Java SE jars using JavaScript?

I need to integrate an API that is written in Java with a Client written in JavaScript. Is there any way to use this API, possibly along with other Java-SE JARs, in Webstorm? Any assistance would be greatly appreciated. Thank you! ...

Displaying the chosen array in a Material UI Table within a React application does not show the desired checkboxes

After days of hard work and research, I finally figured out how to achieve what I needed. In my React App, I have a Material UI table that I want to load with pre-rendered checks in the DOM based on entries in a selected array. The selected array contains ...

Do you think it is advisable to make direct changes to build files in Angular development?

Is it advisable to manually modify build files after deployment in certain scenarios? ...

We are creating a table in JavaScript and mistakenly adding an unnecessary tbody

I am currently utilizing a combination of plain JavaScript and Jquery in order to dynamically generate a table. The issue arises when I attempt to use a for loop to iterate through the data obtained from an Ajax request, as it fails to create new rows. To ...

Guide to showcasing firestore data on a react web application

Hello everyone, I'm new to this forum so please let me know if I have missed any important details. I am trying to figure out how to display data from my Firestore database on my React web application. Currently, I can see the data in the console of t ...

Determine the estimated download duration using the $http protocol

I am experiencing an issue with a function that calculates the time it takes to download a text file (3MB in size) from my server. While it works well for single requests, when I attempt to run multiple requests simultaneously, the time spent waiting for a ...

Check that a string field includes specific characters and digits using JavaScript

I'm in the process of developing a React application that includes an input field requiring a specific format: The first four characters (char 1,2,3, and 4) must be "VTF_" followed by numbers for the remaining part of the string. For example, VTF_12 ...

Preserve selected option in select box after page refresh in JSP

I'm working on a jsp page that contains a simple select html element. Right now, when I select an option and click the Wyswietl button, the page refreshes, the table data is refreshed, but the selected option resets to default... How can I make it sta ...

What could be causing the reliability issue with this particular Angular JS code for dropdown menus?

Attempting to create a dynamic country-city dropdown menu using AngularJS <div> Country: </br> <select data-ng-model="country" ng-options="country.name for country in countries" data-ng-change="updateCountry(country) ...

Utilizing a window.onload function in Microsoft Edge

After trying to run some code post-loading and rendering on the target page, I was recommended to use the Window.load function. This method worked flawlessly in Firefox and Chrome, but unfortunately, I couldn't get it to function in IE. Is there an al ...

Converting a lengthy HTML document into numerous JPEGs (or any preferred image format)

I am working on a lengthy webpage that can be viewed as having 40 individual pages. Each "page" is separated by a horizontal rule () and has a fixed height of 860px. My goal is to convert each of these "pages" into JPG images automatically. Can anyone pr ...

Implementing a 'Load More' button for a list in Vue.js

I am currently working on adding a load more button to my code. While I could achieve this using JavaScript, I am facing difficulties implementing it in Vue.js. Here is the Vue code I have been working with. I attempted to target the element with the compa ...

Loop through the ng-repeat scope object in AngularJS

Is it possible to create rows of 3 using bootstrap and ng-repeat with data from a scope object without the loop repeating every three times due to ng-if? I am curious if something like <h4> {{yogurt[$index+1].name}} </h4> would work. Below is ...

Showing nested arrays in API data using Angular

I would like to display the data from this API { "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", & ...

What is the best way to utilize variables in order to style this image inside a selector?

Struggling with adding dynamic styling to an image inserted into a div through a selector. Despite successful testing of the style changes, I am stuck on how to assign variable values for the properties. Various syntax attempts have failed so far. functi ...

Sorting arrays in JavaScript based on dynamic columns

My current JavaScript Array Sorting code gets the job done, but it feels inefficient. For example, I have an array of objects structured like this: dummyData = []; dummyData.push({ col01:"aa", col02:"ac", col03:"ab" }); dummyData.push({ col01:"ab", col02 ...

Utilizing Node.js to insert data into separate MongoDB schemas

data.js var dataSchema = Schema({ item : String, description : String, category : { type: Schema.Types.ObjectId, ref: 'Category' } }); var Data = mongoose.model('Data&a ...

What prevents Coldfusion from converting a JSON string to a struct or array when deserializing?

I am attempting to deserialize a JSON string using Coldfusion8. Although I am not encountering any errors, I am struggling to work with the retrieved data. This is how my JSON string appears: "{\"kundenliste\":{\"kundennummer\":\ ...