4

if I define a multi-dimentional javascript array like this

//var myStack = new Array(3);  
// *** edit ***
var myStack = {};  

What is the best way to insert one value at a time?

myStack[1][1][0] = myValue;

I want to read a database and write one value at a time. Example:

myStack[recordNo][1]['FirstName'] = myValue;
1
  • 6
    var myStack = new Array(3); does not define a 3-dimensional array, it creates a 1-dimensional array with 3 entries Commented Dec 25, 2011 at 18:05

4 Answers 4

9

Inserting a single value can be done through one line of code:

myStack[1] = [,[value]];

Or, the long-winded way:

myStack[1] = [];
myStack[1][1] = [];
myStack[1][1][0] = value;

Either method will populate the array myStack with multiple arrays, and finally set the desired value, as requested at the question.

EDIT: As a response to the updated question, the following can be used:

myStack[recordNo] = [,{'FirstName': myValue}];
Sign up to request clarification or add additional context in comments.

1 Comment

I see that the question is updated again. If none of the properties are true arrays, you have to use myStack[recordNo] = {1:{firstName: myValue}}; (or, if all of the keys are dynamic: myStack[recordNo] = {};myStack[recordNo][a] = {firstName: myValue};).
5

To avoid dealing with an array index for every dimensions (which could lead to mistakes if you don't pay attention for a second), you can use a push approach. It makes it easier to read / debug, especially when you have a great number of dimensions :

// some constants for navigation purpose
var FIELD_FNAME=0;
var FIELD_LNAME=1;
var FIELD_JOB=2;

// initialize your stack
var myStack=[];

// create a new row
var row = [];

var fname = "Peter";
row.push(fname);

var lname = "Johnson";
row.push(lname);

var job = "Game Director";
row.push(job);

// insert the row
myStack.push(row);

Then it would be possible to iterate like this :

for (var i=0;i<myStack.length;i++) {
    var row = myStack[i];
    var fname = row[FIELD_FNAME];
    var lname = row[FIELD_LNAME];
    var job = row[FIELD_JOB];
}

Comments

2

for example:

var varname={};

for (var k = 0; k < result.rows.length; k++) {
   varname[k] = 
   {
     'id': result.rows.item(k).id,
     'name': result.rows.item(k).name
   };
}

1 Comment

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.
0

You can use three for loops, if you are inserting all of the values into the array at one time:

for (var i = 0; i < 3; i++) {
    for (var j = 0; j < 3; j++) {
        for (var k = 0; k < 3; k++) {
            myStack[i][j][k] = myValue;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.