The Javascript array transforms into null

I have created a two-dimensional array and attempted to fill it using a nested loop. However, only the first dimension is being populated with the correct values while the other dimensions remain filled with null or undefined. Any suggestions on how to rectify this issue would be greatly appreciated.

var Arr = [];
var i = 0;

for (i = 0; i < 13; i++) {
  Arr[i] = [];
}

var a = 0, b = 0;

for (a = 0; a < 13; a++) {
  for (b = 0; b < 13; b++) {
    Arr[[a][b]] = AnotherArrWithRightValues[(13 * a) + b];
  }
}

Answer №1

Instead of Arr[[a][b]], you should write Arr[a][b]

Answer №2

Although Loksly's response is accurate, it offers a fresh approach to achieve the same result. In order to address your inquiry, simply substitute Arr[[a][b]] with Arr[a][b].


Complete Code :

var Arr = [];

for(let a = 0 ; a < 13 ; a++) {
  Arr[a] = [];
  for(let b = 0 ; b < 13 ; b++) {
    Arr[a][b]=AnotherAppropriateArr[(13 * a) + b];
  }
}

Answer №3

Here is an alternative method to achieve the same result:

let myArray = [];
let index = 0, temp;
let x, y;

for(x = 0; x < 13; x++){
    temp = [];
    for(y = 0; y < 13; y++){
       temp.push(valuesFromAnotherArray[index++]);
    }
    myArray.push(temp);
}

Answer №4

Give this a shot:

let newArray = [];

for(let x=0;x<13;x++)
  {
    newArray[x] = new Array();
    for(let y=0;y<13;y++)
      {
        newArray[x].push((13 * x) + y);
      }
  }

Hopefully, this solution serves your needs.

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

Using NodeJS to facilitate communication between multiple NodeJS instances while also overseeing operations of a Minecraft server

I have a question about communicating between NodeJS instances. Let's say I have one NodeJS instance running a chat room - how can I access that chat and see who is connected from another NodeJS instance? Additionally, I'm curious if it's f ...

Challenge with Context Api state not reflecting the latest changes

Hey there, I've got this function defined in AuthContext.js: let [authTokens, setAuthTokens] = useState(null) let [user, setUser] = useState(false) let [failedlogin, setFailedlogin] = useState(false) let loginUser = async (e) => { ...

This browser does not support automatic publicPath in Webpack5

While I was working on webpack 4.44.2, I encountered this error when transitioning to webpack 5.0.0 ERROR in ./src/assets/sass/styles.scss Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): Error: Automatic publicPath is not ...

What guidelines should be followed when creating a custom array in programming?

When dealing with a user defined array, what is the best practice in terms of size and element values? Option A: Start by predefining an array of a certain size (for example 100), then prompt the user to input how many elements they would like to store in ...

Unexpected date outcomes in JavaScript when using a similar date format

Why is it that the first input produces the correct result, while the second input displays a time that is 5 hours behind? new Date("2000-1-1") Sat Jan 01 2000 00:00:00 GMT-0500 (EST) new Date("2000-01-01") Fri Dec 31 1999 19:00:00 GMT-0500 (EST) How can ...

Static IP Address Proxy for Inbound Traffic in App Engine and Cloud Environments

Many cloud platforms, including App Engine, utilize a diverse set of IP addresses, posing challenges for users with firewall restrictions. We have clients interested in our services but they are limited to sending requests to specific IP addresses. Is th ...

How can you efficiently pass multiple JSON files as arguments for Observable Arrays, especially when the values from one file are required for use in another file?

My goal is to utilize $.getJSON to retrieve data from two JSON files and assign their values to observableArrays. Currently, I have hard-coded the JSON data into the observableArray. You can view an example on this JSFiddle link. This was my initial appro ...

The starting point of the Action Sheet is located at the top of the screen in the Android React Native Glue Stack UI

While working on my project using Expo React Native, I incorporated the Glue Stack UI library. However, whenever I added a Popover or Actionsheet component, the content would initially appear at the center or above the screen and then snap back to its inte ...

The functionality for dragging elements is not functioning properly in JavaScript

Here is the code I used in an attempt to make a div element draggable: let div = document.querySelector('div') div.onmousedown = function() { div.addEventListener('mousemove', move, true) } window.onmouseup = function() { window. ...

Accessing Data from an Array in PHP

Let's address the issue at hand. In my array called $datosUsuario['empleadoUsuario'] = $this->EmpleadoModel->get_empleado_vistaUsuario($result[0]->idusuario); To display the values from datosUsuario var_dump($datosUsuario[' ...

Best practice for triggering an Axios post request within a ReactJS application

My form is set up to store data in the state for sending to the backend server. I'm using the handleSubmit function and useEffect hook to handle the form, where handleSubmit ensures validation before submission, and useEffect checks for errors and log ...

Does numpy.multiply always have the same outcome as the * operator?

According to the numpy.multiply documentation: The numpy.multiply function is equivalent to x1 * x2 in terms of array broadcasting. Is there a difference between np.multiply(x1, x2) and x1 * x2 in any scenario? Where can I find the implementation deta ...

Encapsulation tool for C functionality within a C# environment

I am currently in the process of creating a wrapper in order for my C# application to utilize a DLL that was developed in C. One specific method that I am trying to wrap is defined as follows: void methodA(const uint32_t *data); //declaration in C header ...

Execute JavaScript function after the window has finished loading, regardless of any asynchronous downloads

In my app, there is an initialise function that I want to execute only when two conditions are met: first, the window has finished loading ($(window).load()), and second, Typekit has loaded. $(window).load(function() { try { Typekit.load({ act ...

When defining multiple types for props in Vue, the default behavior for Boolean type props is not preserved

Imagine you have a component called MyComponent with a prop named myProp declared as: props: { myProp: Boolean } By simply using <MyComponent myProp/>, the default behavior would set myProp to true. However, this simplicity is lost when there ...

Using JavaScript to insert specific values into an array of floats

let floats = [6, 10, 10, 10, 20, 48, 50.5, 60, 60, 78, 90]; let evens = [{}]; for(let i = 0; i < floats.length; i++){ if(floats[i] == floats[i - 1]) { evens[evens.length - 1][i - 1] = floats[i - 1], evens[evens.l ...

What is the correct way to position a button next to a dropdown list in asp.net?

After dragging a dropdown list onto the page, I attempted to add a button next to it. In the design view, the button appears beside the dropdown list as intended. However, when I run the code, there is more than five spaces between them. I even tried putti ...

Sorted Array Resulting from Execution of For Loop

My goal is to calculate the distance between two sets of latitude and longitude coordinates using the npm geolib library. I have a function that queries Firestore and retrieves an array of objects. Within this array, I iterate through each object in a for ...

Having trouble retrieving the value of a radio button using jQuery?

Here is the link to my jsfiddle: http://jsfiddle.net/rpp6s/ I am trying to display an alert with the value of the second radio button form. However, when I select a value from the first form, the alert shows that value instead of the one from the second ...

The way jQuery and CSS are rendered in the same browser window can vary depending on whether they are loaded via Django or statically

Experiencing a perplexing dilemma. While viewing a web page created using html, jquery, and css on the same browser but in two separate tabs, I am encountering varying renderings of the page. Scenario 1: Directly loading the html file from the file system ...