0

I have a scenario in which i've to store array object in a array.My Javascript code is

var myArray= new Array(5,5);
for(dataIndex in data.data){
   for(myIndex in data.data[dataIndex].myObject){
     var xrow =data.data[dataIndex].myObject[myIndex].row;
     var xcolumn =data.data[dataIndex].myObject[myIndex].column; 
     myarray[xrow][xcolumn] = data.data[dataIndex].myObject[myIndex];  
   }

}

but could not store any data object in the array.Can anyone help me out sort this?

3 Answers 3

1

It looks like you're coming from PHP, where an array is both a sequence of elements and/or key-value pairs? An array in javascript is just a sequence. (Actually, that's not 100% true, but it is for all intents and purposes.) What you want is an object. An object is a series of key value pairs. The keys and values can be any object, from a string to an array to a function.

var myObj = {};
// or assigning properties up front
var myOtherObj = {'foo': 'bar', 'baz': 12 };    
Sign up to request clarification or add additional context in comments.

1 Comment

Can u tell me ow can i declare a multidimentional array say myarray with 5 rows and 3 columns in javascript
0

My problem was not declaring and setting value for the javascript array.I was able to acheive it by this.

var YourArrayHere = new Array();
YourArrayHere.length = [first dimension array length here];
for(var count = 0; count < YourArrayHere.length; count++)
{
  var TempSecondArray = new Array();
  TempSecondArray.length = [sec dimension array length here];
  YourArrayHere[count] = TempSecondArray;
}

Comments

0

Ok got a working demo. Took me a while to figure it out. Was fun. It can be optmized.

EDIT: you don't really need a MAX.

jsfiddle: http://jsfiddle.net/Grimbode/7B8CK/1/

var data = {
    "data":[
        {"myObject":[
                {"row":0, "column":0},
                {"row":0, "column":1},
                {"row":0, "column":2},
             ]


        },
        {
            "myObject":[
                 {"row":1, "column":0},
                 {"row":1, "column":1},
                 {"row":1, "column":2}
            ]
        }
    ]
};

var result = new Array();
for(var i = 0; i < data.data.length; i++)
{
    var temp = new Array();
    var row =  (data.data[i].myObject.length > 0) ? data.data[i].myObject[0].row: null;
    for(var j = 0; j < data.data[i].myObject.length; j++)
    {
        console.log('row: ' + data.data[i].myObject[j].row + ', column: ' + data.data[i].myObject[j].column);
        temp[data.data[i].myObject[j].column] = [data.data[i].myObject[j]];

    }
    if(row != null){ result[row] = temp;}
    console.log(result);
}
console.log('Final:');
console.log(result);

jsfiddle: http://jsfiddle.net/Grimbode/7B8CK/

I represented data as best as I could to fit your example

var MAX_X = 10;
var MAX_Y = 10;
var data = {
    "data":[
        {"myObject":[
                {"row":0, "column":0},
                {"row":0, "column":1},
                {"row":0, "column":2},
             ]


        },
        {
            "myObject":[
                 {"row":1, "column":0},
                 {"row":1, "column":1},
                 {"row":1, "column":2}
            ]
        }
    ]
};

var result = new Array(MAX_X);
for(var i = 0; i < data.data.length; i++)
{
    var temp = new Array(MAX_Y);
    var row =  (data.data[i].myObject.length > 0) ? data.data[i].myObject[0].row: null;
    for(var j = 0; j < data.data[i].myObject.length; j++)
    {
        console.log('row: ' + data.data[i].myObject[j].row + ', column: ' + data.data[i].myObject[j].column);
        temp[data.data[i].myObject[j].column] = [data.data[i].myObject[j]];

    }
    if(row != null){ result[row] = temp;}
    console.log(result);
}
console.log('Final:');
console.log(result);

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.