Generate an array containing multiple arrays with variable names

I am working on creating an array for each user and storing them in another array. Here is the code snippet I used for this:

var arrays = [];
var userCounter = 1;
@foreach($eventUsers as $user)
{
   arrays['arr' + userCounter] = [];
   userCounter++;
}

When I console.log(arrays), it showed me the following output:

[arr1: Array[0], arr2: Array[0], arr3: Array[0], arr4: Array[0]]

Now, I need to add elements to each individual array (arr1, arr2, arr3, arr4) while looping through 'arrays'.

I attempted the following approach:

for (arrayName in arrays) {
    arrayName.push('x');
}

Unfortunately, this didn't work as expected. Can anyone provide a solution?

Answer №1

Construct your array

let customArrays = {}, index;
for (index = 0; index < 5; index++) {
    customArrays['customArray' + index] = [];
}

Modify the customArrays

// loop through the keys
for (index in customArrays) {
    // update the values
    customArrays[index].push('updatedValue');
};

Answer №2

Encountered a solution...

 for (index in arrays) {
  arrays[index].push("RRRRR");
}

Answer №3

What you need is a specific object:

var myObject = {};
myObject.name = "John";
myObject["age"] = 30;
console.log(myObject);

Refer to this article for guidance on how to iterate through an object, and visit this page for additional information.

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

What is the method for setting up a constant array at a designated memory location?

In dealing with embedded controllers, I am looking to initialize a constant array in memory. Specifically, I want to store this array at a predetermined location such as 0x8000. By doing so, I hope to reserve a portion of code memory that can be erased lat ...

What is the best way to retrieve the offsetHeight of a Component Element using Vue.js?

I am currently working on a Vue.js component and successfully inserting it into the DOM. My goal is to find out its rendered height once it's in the DOM, specifically its offsetHeight. However, I seem to be missing something obvious as I can't fi ...

javascript execute process and make a function call

I'm experiencing a problem with JavaScript. When I click a button, it calls a function. function onButtonClickFunction() { ajaxCall(); } function ajaxCall() { $('.black_overlay').show(); /* Some Ajax Code */ $('.blac ...

What does the error message "TypeError: Bad argument TypeError" in Node's Child Process Spawn mean?

Every time I execute the code below using node: var command = "/home/myScript.sh"; fs.exists(command, function(exists){ if(exists) { var childProcess = spawn(command, []); //this is line 602 } }); I encounter this error: [critical e ...

How can we dynamically navigate to the next line within the Material UI DataGrid component when space is limited?

Currently, I am working with the datagrid component from material ui. I have retrieved some data from a database and am attempting to pass it to the datagrid component. However, I have noticed that certain fields contain long strings which do not fully app ...

Replacing the $http.get function in AngularJS

Here is the code snippet I am currently working with: $http.get(url).success(function(response,status,header,config) { $scope.mymodel = response; } I need to verify the http status and trigger a function accordingly. Modifying all 100 instances of ht ...

Tips on displaying a div element using jQuery

What is required: I simply need to display a div. Code snippet: <a href="javascript:void(0);" id="viewdetail" onclick="$(this).show();" style="color:green">View Detail <div class="speakers dis-non"> </div> </a> After ...

Is it advisable to transpile my Node.js code in order to utilize ES6 features?

My focus in using Node.js is solely for server-side microservices. I'm interested in incorporating ES6 into my code, but I've come across information suggesting that Babel is necessary to transpile the code to ES5 for browser compatibility. My qu ...

My Ruby on Rails app seems to be malfunctioning in a major way

Instead of sharing code snippets, I’ve encountered difficulties in getting my jQuery code to work correctly. If you're curious, you can view the issues on Stack Overflow: Why doesn’t this jQuery code work? and This jQuery hide function just does n ...

Fetch a document from a NodeJS Server utilizing Express

Is there a way to download a file from my server to my machine by accessing a page on a nodeJS server? I am currently using ExpressJS and I have attempted the following: app.get('/download', function(req, res){ var file = fs.readFileSync(__d ...

Converting API data to JSX layout

Here is a sample JSON response that I have: [ { "author": 2, "title": "how to draw", "slug": "how-to-draw", "content": "second attempt", " ...

Having trouble displaying information in a table using React JS

I devised a feature to display one column of a table and one column for checkboxes (each row should have a checkbox). I stored this file in a component folder with the intention of creating a page where the user selects an account type, and then a new tabl ...

Why is the updated index.html not loading with the root request?

I'm currently working on an Angular app and facing an issue with the index.html file not being updated when changes are made. I have noticed that even after modifying the index.html file, requests to localhost:8000 do not reflect the updates. This pro ...

Is it possible to create a clip plane in Three.js for objects that intersect?

Currently, my setup involves using a PlaneGeometry as the representation of water. I have also added a ship (gltf model) onto the water. The issue I'm encountering is that when the boat slightly overlaps with the water surface, the water is visible in ...

Implementing objects in a three.js scene without displaying them on the screen

I have a function called createCylinder(n, len, rad) that is being invoked from another function named createScene(). Despite checking that the vertices and faces are correctly added without errors, the geometry itself is not rendering. I suspect this is ...

Transfer pictures from an iframe to a textarea

Is there a way to copy images from an iframe to a textarea using JavaScript even when the pages and iframe are not on the same domain or server? If you have any suggestions or solutions, please share! The iframe containing the images is utilizing ajax to ...

Is there a way to style the current page's link to make it appear disabled?

On my website, there are three main pages: Home, About, and Contact. I want the link to the current page to visually indicate that clicking on it would be redundant since the user is already on that page. Should I use CSS or jQuery for this task? And what ...

Express.js applications may encounter issues with static files and scripts not being found when utilizing dynamic endpoints

I've run into a snag with my Express.js application regarding the inability to locate static files and scripts when accessing certain endpoints. Here's what's happening: My Express.js app has multiple routes serving HTML pages along with st ...

Auto-adjusting height container following animation

Through javascript, I am able to adjust the height of my element from 0 to auto as I was unable to accomplish this using CSS. /* Function for animating height: auto */ function autoHeightAnimate(element, time){ var curHeight = element.height(), // Get ...

Moving an array to the right solely through addresses and pointers, without the use of indexes (thread closed)

I am currently facing a task of shifting an array of real numbers to the right by n elements. After initially solving it, I realized that I was expected to use only addresses and pointers. I attempted to rewrite the code using addresses, but it does not se ...