JavaScript How to initialize 4d array in javascript? initialize array javascript with values,initialize array javascript with size,

Here is my code:

var arr = [[[[[]]]]];
var c = 20;
for (i=0;i<5;i++)
    arr[i][0][0][0] = c;
alert(arr[2][0][0][0]);

It doesn't work, but how can I do this?

Answer:1

Most people here are using for loops, which I think are mostly obsolete in the age of anonymous functions in JavaScript. You people should know better :P

Anyway, you can solve this quite nicely in a one-liner. Here are a few scripts that can initialize your array...

If you already have a 4-dimensional array, you can initialize it elegantly like this:

arr.forEach(function(e) { e[0][0][0] = c })

Or, if you're more into map:

arr.map(function(e) { e[0][0][0] = c })

These are assuming you already have c defined, which you do in your code sample (20).

From now on, though, please Google your questions before asking them on stackoverflow. You will receive an answer that has already been accepted :)

Answer:2

It doesn't work because you haven't specified any elements beyond the first one, so the length of array is one and accessing further keys is incorrect.

I think, the most convenient way would be to push a new 3d array with c inside on every iteration (actually I have no idea what you're trying to achieve with this xD):

var arr = [];
var c = 20;
for (i=0;i<5;i++) 
    arr.push([[[c]]])

alert(arr[2][0][0][0]);
Answer:3

It is unclear what you want, but I imagine a 4 dimension array is an array that has a set of arrays nested 3 deep, each of which has an array nested 2 deep, each of which has a single array that contains values.

In a one dimension array, you access the value at index 2 by:

arr[2];

In a two dimension array, you'd access the value at (2,3) by:

arr[2][3]

and so on until you get to the value at (2,3,1,2) in a four dimension array by:

arr[2][3][1][2]

and if that was the only value in the array, it would look like:

[,,[,,,[,[,,'value at 2312']]]];

If there was also a value at (1,1,0,2) the array would now look like:

[,[,[[,,'value at 1102']]],[,,,[,[,,'value at 2312']]]];

There can only be values in the last nested array, the value at indexes in every other array must be another array (for the lower dimensions), so to insert at value at, say (2,1,3,1) and assign it a value of 6, you need to loop over the array and inspect each index. If it's not already an array, insert an array and keep going, e.g.:

// Insert value in arrary at coord
// coord is a comma separated list of coordinates.
function insertValue( array, coord, value) {
  var coords = coord.split(',');
  var arr = array;

  for (var c, i=0, iLen=coords.length-1; i < iLen; i++) {
    c = coords[i];
    
    if (!Array.isArray(arr[c])) arr[c] = [];
    arr = arr[c];
  }
  arr[coords[i]] = value;
  return array;
}

document.write('result: ' + JSON.stringify(insertValue([],'1,2,1,3','at 1213')));
Answer:4

What is the best way to perform a NOT operation? For example, here's the javascript Template.mytemplate.helper({ myhelper : function(){ return true; } }); Now how would I get !myhelper in a ...

What is the best way to perform a NOT operation? For example, here's the javascript Template.mytemplate.helper({ myhelper : function(){ return true; } }); Now how would I get !myhelper in a ...

  1. helper t cells
  2. helper helper
  3. helper utah
  4. helper synonym
  5. helper springs
  6. helper t cells function
  7. helper t cells definition
  8. helpers inc
  9. helper jobs
  10. helper monkey
  11. helper definition
  12. helper act
  13. helper clinic
  14. helper tools for instagram
  15. helper in spanish
  16. helper utah weather
  17. helper stool
  18. helper tower
  19. helper jobs near me
  20. helper crossword clue

I am currently trying to get my head around database's and web programming. I currently have 3 files index.html, script.js (empty at the minute) and test_db.sqlite. I am trying to pull info from the ...

I am currently trying to get my head around database's and web programming. I currently have 3 files index.html, script.js (empty at the minute) and test_db.sqlite. I am trying to pull info from the ...

  1. sqlite reading writing

Run the code up to and including plotly_build(p) and the correct plot results. Reproducible Code library(plotly) #data df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ), ...

Run the code up to and including plotly_build(p) and the correct plot results. Reproducible Code library(plotly) #data df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ), ...

exports.index = function(req, res) { moviedb.indexMovie() .then(x => { Movie.findAsync() .then(responseWithResult(res)) .catch(handleError(res)) } ) }; function ...

exports.index = function(req, res) { moviedb.indexMovie() .then(x => { Movie.findAsync() .then(responseWithResult(res)) .catch(handleError(res)) } ) }; function ...

  1. javascript anonymous function with parameters
  2. javascript anonymous function this
  3. javascript anonymous function vs named
  4. javascript anonymous function return
  5. javascript anonymous function arrow
  6. javascript anonymous function example
  7. javascript anonymous function call
  8. javascript anonymous function scope
  9. javascript anonymous function return object
  10. javascript anonymous function callback
  11. javascript anonymous function syntax
  12. javascript anonymous function async
  13. javascript anonymous function arrow syntax
  14. javascript anonymous function es6
  15. javascript anonymous function closure
  16. javascript anonymous function object
  17. javascript anonymous function as argument
  18. javascript anonymous function onclick
  19. javascript anonymous function use
  20. javascript anonymous function foreach