-3

There is an array:

var data = data.addRows([
        ['Nitrogen', 0.78],
        ['Oxygen', 0.21],
        ['Other', 0.01]
      ]);

What does it mean? Arrays in array? What is it ['Nitrogen', 0.78] ? Why [] brackets?

I tried to reproduce this like:

var arr = [];
arr.push({'Oxygen', 0.21});
POV
  • 11,293
  • 34
  • 107
  • 201
  • 1
    you should do arr.push(['Oxygen', 0.21]); – marvel308 Sep 25 '17 at 20:57
  • 1
    "*What does it mean? Arrays in array?*". It's exactly that - an array with multiple arrays contained within that array (otherwise known as a multidimensional array). – Obsidian Age Sep 25 '17 at 20:57
  • 2
    `{'Oxygen', 0.21}` is a syntax error, as you've discovered. To reproduce it you'd write `arr.push(['Oxygen', 0.21]);` – Pointy Sep 25 '17 at 20:57
  • [This question/answer](https://stackoverflow.com/questions/7545641/javascript-multidimensional-array) may help. – TheOdd Sep 25 '17 at 20:59
  • I tried push like: `var item = [key, value];` – POV Sep 25 '17 at 21:20

2 Answers2

2

That is an array of arrays. Otherwise known as a multidimentional array. The [] inside the [] backets indicate a seperate arrays.

Pointy has already answered this in the comments but I will put it here.

arr.push({'Oxygen', 0.21}); is pushing an object to an array, which is denoted by the {} curly braces (However if you did want to push an object to an array the syntax would actually be arr.push({'Oxygen' : 0.21});). In order to push an array to array you would need to do arr.push(['Oxygen', 0.21]);, which is using [] square brackets.

WizardCoder
  • 3,353
  • 9
  • 20
1

yes it's array within an array; if you want to reproduce it should be like the following

var arr = [];
arr.push(['oxygen',0.21]);
// to see what's inside 
console.log(JSON.stringify(arr));

// to check the type 
for ( var i = 0 ; i < arr.length ; i++ ){
 console.log('array[',i,'] = ', typeof(arr[i]));
}
johnjerrico
  • 143
  • 1
  • 9