2
var data = {
     row: row,
     row2: row2
};

var tableData = [data.row,data.row2];

row objects contains lot of children objects too... now how would i placed my tableData to access each single objects. so it goes like this...

var tableData = [data.row[1],data.row[2],data.row[3],,data.row2];

Updated Question

var data = [row, row2];

In this case how would i access my row children objects.

1

3 Answers 3

1
var data = {key: value, ... };

var tableData = [];
for (var k in data) {
    for (var i = 0, len = data[k].length; i < len; i++)) {
        tableData.push(data[k][i]);
    }
}

Use nested for loops and array.prototype.push

Edit

for (var j = 0, len = data.length;j < len;j++) {
    for (var i = 0, len = data[j].length; i < len; i++)) {
        tableData.push(data[j][i]);
    }
}

You can replace for (var j in data) with for (var j = 0, len = data.length; j < len; j++)

The latter

  • sets j to 0
  • caches the length so you only ask for the length once.
  • Checks that j is not bigger then the amount of elements in your array
  • Increases j when you get to the end of the block.
Sign up to request clarification or add additional context in comments.

4 Comments

One question what if my data is not an Json Object and instead its an Array of Object like this var data = [row, row2];
@TheJava then it still works. As long as each row is also an Array. You may want to convert the for in loop into normal for loop.
@theJava how lazy are you? it's a simple change. Take your hands out of your pocket.
if i had know i would have not asked
1

Assign data.row first, then push data.row2, like this:

var tableData = data.row
tableData.push(data.row2)

5 Comments

That's the same as var tableData = [data.row,data.row2]; he already got that far.
Not the same, assuming row1 = [a,b,c]; row2 = [d] his produces [[a, b, c], d] and mine produces [a, b, c, d] which I think is what he is after.
true that would work. But only in a very specific case, that does not scale beyond his trivial example.
@Raynos I am only trying to answer his question here - answers to other questions can found on other question's pages. The example given may not be trivial to @theJava.
I meant he gave an example of his data rather then the actual data.The actual data probably has more elements.
1

How about something like this...

var data = {
    letters : ["a", "b", "c"],
    numbers : ["0", "1", "2", "3"]
};

data.letters; // ["a", "b", "c"]
data.letters[0]; // "a"
data.numbers; // "0", "1", "2", "3"]
data.numbers[2]; // "2"

... or you could try this...

var data = {
    letters : new Array("a", "b", "c"),
    numbers : new Array("0", "1", "2", "3")
};

data.letters; // ["a", "b", "c"]
data.letters[0]; // "a"
data.letters.push("d"); // the array is now ["a", "b", "c"]

data.numbers; // ["0", "1", "2", "3"]
data.numbers[2]; // "2"
data.numbers.pop(); // the array is now ["0", "1", "2"]

For more info on JavaScript arrays, check out these links:

I hope this helps.
Hristo

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.