I have this JSON data:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
Suppose I don't know how many columns and rows of employees I have, how do I create this object in JavaScript (Without concate strings)? Assume that I get each row in "onGeneratedRow" method, and I need to push each column (firstName, lastName) to the '{}' brackets.
var viewData = {
employees : []
};
var rowNum = -1;
function onGeneratedRow(columnsResult)
{
rowNum = rowNum + 1;
viewData.employees.push({});
columnsResult.forEach(function(column) {
var columnName = column.metadata.colName;
viewData.employees[rowNum][columnName] = column.value; });
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…