将 id-keyed 对象转换为数组
Convert id-keyed objects into array
我有以下带有嵌套对象的对象,这些嵌套对象由自己的 ID 作为其键标识。我不认为这是使用 Angular 的正确方法,因此我需要摆脱 ID 键,因为每个对象都有自己的 ID 作为其 属性。
{
"1": {
"id": 1,
"questionGroup": "Mathematics",
"questions": {
"1": {
"id": 1,
"questionTitle": "2+2",
"questionType": "singleChoice",
"answerOptions": {
"1": {
"id": 1,
"answerOptionText": "4"
},
"2": {
"id": 2,
"answerOptionText": "3"
}
}
}
}
},
"2": {
"id": 2,
"questionGroup": "Geography",
"questions": {
"3": {
"id": 3
}
}
},
"3": {
"id": 3,
"questionGroup": "Philosophy",
"questions": {
"4": {
"id": 4
}
}
}
}
但我需要将其转换成下面的形式:
[
{
"id": 1,
"questionGroup": "Mathematics",
"questions": [
{
"id": 1,
"questionTitle": "2+2",
"questionType": "singleChoice",
"answerOptions": [
{
"id": 1,
"answerOptionText": "4"
},
{
"id": 2,
"answerOptionText": "3"
}
]
}
]
},
{
"id": 2,
"questionGroup": "Geography",
"questions": [
{
"id": 3
}
]
},
{
"id": 3,
"questionGroup": "Philosophy",
"questions": [
{
"id": 4
}
]
}
]
我希望看到使用最 JavaScript 现代方式来处理这个问题的答案,因为我不喜欢使用我的老式方式处理许多嵌套迭代器和条件。
json_result = [];
db.query(`SELECT ex.id, ex.name, qgrp.id as questionGroupId, qgrp.name as questionGroupName, ans.id as answerOptionId, ans.\`text\` as questionName, ans.question_type as questionType, ansopt.id as answerOptionId, ansopt.display_text as answerOptionText FROM _questionGroup qgrp
INNER JOIN exam ex
ON ex.id = qgrp.examId
INNER JOIN question ans
ON ans.questionGroupId = qgrp.id
INNER JOIN answerOption ansopt
ON ansopt.answerOptionId = ans.id
AND ex.id = ?;`, [req.params.id], (err, rows, results) => {
if(!err) {
var index = {}, questionGroup = {}, question = {};
rows.forEach(function (row) {
console.log("ROW: ", row);
if ( !(row.id in index) ) {
index[row.id] = {
id: row.id,
name: row.name,
questionGroup: {}
};
json_result.push(index[row.id]);
}
if( !(row.questionGroupId in questionGroup) ) {
questionGroup[row.questionGroupId] = {
id: row.questionGroupId,
name: row.questionGroupName,
question: {}
};
index[row.id].questionGroup[row.questionGroupId] = questionGroup[row.questionGroupId];
}
if( !(row.answerOptionId in question) ) {
question[row.answerOptionId] = {
id: row.answerOptionId,
name: row.questionName,
question_type: row.questionType,
answerOptions: []
}
}
question[row.answerOptionId].answerOptions.push({
id: row.answerOptionId,
name: row.answerOptionText,
});
index[row.id].questionGroup[row.questionGroupId].question[row.answerOptionId] = question[row.answerOptionId];
});
res.status(200).json(json_result);
} else {
res.status(410).json(err);
}
}
);
@Keith 的评论帮助找到了解决方案,即将添加 Object.values(json_result)
(Reference) 到响应数据。如下:
json_result = [];
db.query(`SELECT ex.id, ex.name, qgrp.id as questionGroupId, qgrp.name as questionGroupName, ans.id as answerOptionId, ans.\`text\` as questionName, ans.question_type as questionType, ansopt.id as answerOptionId, ansopt.display_text as answerOptionText FROM _questionGroup qgrp
INNER JOIN exam ex
ON ex.id = qgrp.examId
INNER JOIN question ans
ON ans.questionGroupId = qgrp.id
INNER JOIN answerOption ansopt
ON ansopt.answerOptionId = ans.id
AND ex.id = ?;`, [req.params.id], (err, rows, results) => {
if(!err) {
var index = {}, questionGroup = {}, question = {};
rows.forEach(function (row) {
console.log("ROW: ", row);
if ( !(row.id in index) ) {
index[row.id] = {
id: row.id,
name: row.name,
questionGroup: {}
};
json_result.push(index[row.id]);
}
if( !(row.questionGroupId in questionGroup) ) {
questionGroup[row.questionGroupId] = {
id: row.questionGroupId,
name: row.questionGroupName,
question: {}
};
index[row.id].questionGroup[row.questionGroupId] = questionGroup[row.questionGroupId];
}
if( !(row.answerOptionId in question) ) {
question[row.answerOptionId] = {
id: row.answerOptionId,
name: row.questionName,
question_type: row.questionType,
answerOptions: []
}
}
question[row.answerOptionId].answerOptions.push({
id: row.answerOptionId,
name: row.answerOptionText,
});
index[row.id].questionGroup[row.questionGroupId].question[row.answerOptionId] = question[row.answerOptionId];
});
res.status(200).json(Object.values(json_result));
} else {
res.status(410).json(err);
}
}
);
我有以下带有嵌套对象的对象,这些嵌套对象由自己的 ID 作为其键标识。我不认为这是使用 Angular 的正确方法,因此我需要摆脱 ID 键,因为每个对象都有自己的 ID 作为其 属性。
{
"1": {
"id": 1,
"questionGroup": "Mathematics",
"questions": {
"1": {
"id": 1,
"questionTitle": "2+2",
"questionType": "singleChoice",
"answerOptions": {
"1": {
"id": 1,
"answerOptionText": "4"
},
"2": {
"id": 2,
"answerOptionText": "3"
}
}
}
}
},
"2": {
"id": 2,
"questionGroup": "Geography",
"questions": {
"3": {
"id": 3
}
}
},
"3": {
"id": 3,
"questionGroup": "Philosophy",
"questions": {
"4": {
"id": 4
}
}
}
}
但我需要将其转换成下面的形式:
[
{
"id": 1,
"questionGroup": "Mathematics",
"questions": [
{
"id": 1,
"questionTitle": "2+2",
"questionType": "singleChoice",
"answerOptions": [
{
"id": 1,
"answerOptionText": "4"
},
{
"id": 2,
"answerOptionText": "3"
}
]
}
]
},
{
"id": 2,
"questionGroup": "Geography",
"questions": [
{
"id": 3
}
]
},
{
"id": 3,
"questionGroup": "Philosophy",
"questions": [
{
"id": 4
}
]
}
]
我希望看到使用最 JavaScript 现代方式来处理这个问题的答案,因为我不喜欢使用我的老式方式处理许多嵌套迭代器和条件。
json_result = [];
db.query(`SELECT ex.id, ex.name, qgrp.id as questionGroupId, qgrp.name as questionGroupName, ans.id as answerOptionId, ans.\`text\` as questionName, ans.question_type as questionType, ansopt.id as answerOptionId, ansopt.display_text as answerOptionText FROM _questionGroup qgrp
INNER JOIN exam ex
ON ex.id = qgrp.examId
INNER JOIN question ans
ON ans.questionGroupId = qgrp.id
INNER JOIN answerOption ansopt
ON ansopt.answerOptionId = ans.id
AND ex.id = ?;`, [req.params.id], (err, rows, results) => {
if(!err) {
var index = {}, questionGroup = {}, question = {};
rows.forEach(function (row) {
console.log("ROW: ", row);
if ( !(row.id in index) ) {
index[row.id] = {
id: row.id,
name: row.name,
questionGroup: {}
};
json_result.push(index[row.id]);
}
if( !(row.questionGroupId in questionGroup) ) {
questionGroup[row.questionGroupId] = {
id: row.questionGroupId,
name: row.questionGroupName,
question: {}
};
index[row.id].questionGroup[row.questionGroupId] = questionGroup[row.questionGroupId];
}
if( !(row.answerOptionId in question) ) {
question[row.answerOptionId] = {
id: row.answerOptionId,
name: row.questionName,
question_type: row.questionType,
answerOptions: []
}
}
question[row.answerOptionId].answerOptions.push({
id: row.answerOptionId,
name: row.answerOptionText,
});
index[row.id].questionGroup[row.questionGroupId].question[row.answerOptionId] = question[row.answerOptionId];
});
res.status(200).json(json_result);
} else {
res.status(410).json(err);
}
}
);
@Keith 的评论帮助找到了解决方案,即将添加 Object.values(json_result)
(Reference) 到响应数据。如下:
json_result = [];
db.query(`SELECT ex.id, ex.name, qgrp.id as questionGroupId, qgrp.name as questionGroupName, ans.id as answerOptionId, ans.\`text\` as questionName, ans.question_type as questionType, ansopt.id as answerOptionId, ansopt.display_text as answerOptionText FROM _questionGroup qgrp
INNER JOIN exam ex
ON ex.id = qgrp.examId
INNER JOIN question ans
ON ans.questionGroupId = qgrp.id
INNER JOIN answerOption ansopt
ON ansopt.answerOptionId = ans.id
AND ex.id = ?;`, [req.params.id], (err, rows, results) => {
if(!err) {
var index = {}, questionGroup = {}, question = {};
rows.forEach(function (row) {
console.log("ROW: ", row);
if ( !(row.id in index) ) {
index[row.id] = {
id: row.id,
name: row.name,
questionGroup: {}
};
json_result.push(index[row.id]);
}
if( !(row.questionGroupId in questionGroup) ) {
questionGroup[row.questionGroupId] = {
id: row.questionGroupId,
name: row.questionGroupName,
question: {}
};
index[row.id].questionGroup[row.questionGroupId] = questionGroup[row.questionGroupId];
}
if( !(row.answerOptionId in question) ) {
question[row.answerOptionId] = {
id: row.answerOptionId,
name: row.questionName,
question_type: row.questionType,
answerOptions: []
}
}
question[row.answerOptionId].answerOptions.push({
id: row.answerOptionId,
name: row.answerOptionText,
});
index[row.id].questionGroup[row.questionGroupId].question[row.answerOptionId] = question[row.answerOptionId];
});
res.status(200).json(Object.values(json_result));
} else {
res.status(410).json(err);
}
}
);