Create a new array in Javascript with random indexes

Trying to populate an array randomly with rows and columns:

rows = ['1','0','2','1','3'];
columns = ['0','0','1','2','3'];
butArray = [];

Initially, the button array is empty. The goal is to assign values from butArray to printArr based on corresponding row and column indices.

printArr[rows[i]][columns[i]] = butArray[i];

What would be the most effective way to implement a loop for this process?

Answer №1

Here is what you have been searching for.

https://plnkr.co/edit/1J2VWX0yCIWITQp6Yax8?p=preview

// Add your code below

function myclick(){
  console.log("cds")
  rows = ['1','0','2','1','3'];
  columns = ['0','0','1','2','3'];
  butArray = ['qwe','qwe','qewe','qew','qwe'];//some values
  var printArr = [];
  for(var i=0 ; i<columns.length ; i++){
    if(printArr[rows[i]] == undefined)
    printArr[rows[i]] = []
    printArr[rows[i]][columns[i]] = butArray[i];

  }
  console.log(printArr);
}

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

The email was sent successfully using AJAX POST, however, the error callback was triggered instead of the success callback

When I send e-mails via AJAX, it successfully goes to the recipients. However, I'm puzzled as to why the success callback in AJAX is not triggered even though the e-mail has been sent. Instead, it triggers an error callback. It doesn't seem to b ...

What is the process for including or excluding a class from a horizontal scrollbar?

I've been trying to implement a back to top button on a horizontally scrolling page. However, I'm encountering difficulties in adding or removing the necessary class to show or hide the button. Here's the JavaScript code I'm using: $( ...

Defining a Class in Javascript with an Array Property

After successfully writing the code in Java, I am now attempting to write it in JavaScript. Here is the current Java code: public class CarGallery { static int carCounter=10; static Gallery[] car = new Gallery[carCounter]; public static voi ...

How to isolate a function in React when mapping data

I am currently working on mapping data as a repeater in my React project. However, I am facing an issue with isolating the opening function, specifically with an accordion component. As I continue to learn React, I want to ensure that each accordion operat ...

What is the best method for generating individual, randomized triangles?

Check out this script for creating triangles Here's what happens with this script on every frame: Retrieve the slider value If the slider value changes, dynamically adjust the sizes of Float32Array arrays for vertices and colors Iterate through a l ...

Use HTML to showcase an image that dynamically changes based on the outcome of a query function

Hello there, I hope my inquiry is clear enough. I apologize for reaching out as I am unsure where to begin and what exactly I should be focusing on. Currently, I have an image displayed in an HTML page like this: <div id="tag_sunrise_sunset"><p ...

Is there a way to view the contents of the dev server once it has been bundled by webpack?

From my understanding, webpack in dev mode stores all imported files in a certain location and then serves the bundle.js file to the client. If the code inside bundle.js requests a CSS file, the css-loader should have already been configured to provide t ...

What is the best way to rate a particular image?

while ($row = mysqli_fetch_array($result)) { echo ""; echo "<div id='img_div'>"; echo "<i class='fas fa-times'></i>"; echo "<img class='photoDeGallery' s ...

Modify content of elements by clicking on them with jQuery

Currently, there is a button that triggers the loading of an HTML page into a div with the ID "myDiv". Once loaded, I would like to be able to change the text content of specific tags when they are clicked. For example, clicking on the text within an h2 ta ...

Integrating HTML elements based on scroll movement

Is there a way to dynamically add a new Bootstrap row to my HTML code when scrolling the mouse downward using JavaScript? <div class="row"> <div class="col-md-6 col-xs-6" > <div> <img src="img/picture1.jpg" a ...

Remove HTML element and launch in a separate browser tab while preserving styles

I am currently working on developing a single-page application with Polymer3 (Javascript ES6 with imports). One of the key functionalities of my application involves moving widgets to new browser windows, allowing users to spread them across multiple scree ...

Enhance your deletion process by utilizing Ajax for smooth communication and incorporating a

When a user clicks on the delete ImageButton, I want to display a confirmation message. If they select 'Ok', then the deletion will proceed. If 'Cancel' is clicked, nothing will happen. <asp:ImageButton ID="ImageButton1" runat="serv ...

What is the best way to update the current route in Angular 2 programmatically?

In my Angular 2 application, I am facing an issue with the navigation flow between the home component and the nav panel component. When a user clicks on the logout button in the nav panel, the current URL is correctly shown as "/login" in the console log. ...

JavaScript conditional array.join operation

I've got an array of strings that I need to display as a comma-separated list, with "and" added before the last element. Here's what I have: const fruits = ["Banana", "Orange", "Apple", "Mango"]; const energy = fruits.join(', ').replac ...

Refreshed the page following a setAttribute function call in Javascript

I have successfully implemented a function to switch between light and dark mode on my webpage using VueJS. However, I am facing an issue with the code: <a @click="toggleTheme" class="switch-mode" href> <div class=&q ...

Enhancing Nested Objects using Mongoose

When attempting to update nested objects using Mongoose, the request I receive appears like this: { 'example[apple]': 'false', 'example[pear]': 'false', 'example[banana]': 'false', 'example[ ...

Learn how to extract an entire table from a website using Kimono Labs, rather than just the first ten rows

Currently, I am utilizing Kimono labs to generate an API for scraping data from the table on this specific website. However, the website only displays the initial 10 rows of the table by default, limiting my API output to just 10 rows. Is there a method to ...

Selecting hidden elements in jQuery with the exception of

Typically, jQuery Validation does not validate hidden elements: ignore (default: ":hidden") Type: Selector Elements to exclude during validation, filtering them out. jQuery’s not-method is utilized, therefore any valid selector for not() can be used ...

In situations where there may be a duplicate, what alternative can I utilize in place of the id attribute?

I understand that almost any element in the DOM can have an "id" attribute, and I've used it to track each client in a table of clients. Although ids should not be repeated, my rows are assigned unique identifiers based on each person's "clientId ...

Encountering an issue with resolving a local variable during the execution of a test with Protractor

I'm currently learning about async calls in protractor as a newbie to the tool. I am struggling to figure out how to handle a boolean variable that I set to true if any test case fails and the execution goes to the catch block for a promise. Below is ...