Accessing objects within objects
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 wou开发者_JAVA百科ld i access my row children objects.
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.
Assign data.row first, then push data.row2, like this:
var tableData = data.row
tableData.push(data.row2)
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:
- http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
- http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/
I hope this helps.
Hristo
精彩评论