通过下划线 js 将 json 个带有键的对象分组
Group json objects with keys by underscore js
我有如下 JSON 对象,我试图通过第一组键来分组和推送这个对象。
main = {"answer_options":{
"1":{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"},
"2":{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}},
"question":
{"1":"The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n","2":"Latitude of a place is the angular distance from<\/p>\r\n"}
};
我试图获得如下输出。任何人都可以提供帮助吗?
{"1" :
{{"1" : "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n"},
{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"}},
{"2":
{{"2" : "Latitude of a place is the angular distance from<\/p>\r\n"},
{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}}
}
您预期的输出格式并不是可取的(或常用的)格式。这是一种替代方法。您可以使用 for
/ in
遍历问题并将其与相应的答案选项匹配。
main = {
"answer_options": {
"1": {
"1": "optical projection<\/p>\r\n",
"2": "optical mechanism projection<\/p>\r\n",
"3": "mechanical projection<\/p>\r\n",
"4": "all the above<\/p>\r\n"
},
"2": {
"5": "Greenwich to the place<\/p>\r\n",
"6": "equator to the poles<\/p>\r\n",
"7": "equator to the nearer pole<\/p>\r\n",
"8": "equator to the nearer pole along the meridian of the place<\/p>\r\n",
"9": "none of these.<\/p>\r\n"
}
},
"question": {
"1": "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n",
"2": "Latitude of a place is the angular distance from<\/p>\r\n"
}
};
var newMain = {};
for (var key in main.question) {
newMain[ key ] = [
main.question[key],
main.answer_options[key]
];
}
console.log(newMain);
我有如下 JSON 对象,我试图通过第一组键来分组和推送这个对象。
main = {"answer_options":{
"1":{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"},
"2":{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}},
"question":
{"1":"The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n","2":"Latitude of a place is the angular distance from<\/p>\r\n"}
};
我试图获得如下输出。任何人都可以提供帮助吗?
{"1" :
{{"1" : "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n"},
{"1":"optical projection<\/p>\r\n","2":"optical mechanism projection<\/p>\r\n","3":"mechanical projection<\/p>\r\n","4":"all the above<\/p>\r\n"}},
{"2":
{{"2" : "Latitude of a place is the angular distance from<\/p>\r\n"},
{"5":"Greenwich to the place<\/p>\r\n","6":"equator to the poles<\/p>\r\n","7":"equator to the nearer pole<\/p>\r\n","8":"equator to the nearer pole along the meridian of the place<\/p>\r\n","9":"none of these.<\/p>\r\n"}}
}
您预期的输出格式并不是可取的(或常用的)格式。这是一种替代方法。您可以使用 for
/ in
遍历问题并将其与相应的答案选项匹配。
main = {
"answer_options": {
"1": {
"1": "optical projection<\/p>\r\n",
"2": "optical mechanism projection<\/p>\r\n",
"3": "mechanical projection<\/p>\r\n",
"4": "all the above<\/p>\r\n"
},
"2": {
"5": "Greenwich to the place<\/p>\r\n",
"6": "equator to the poles<\/p>\r\n",
"7": "equator to the nearer pole<\/p>\r\n",
"8": "equator to the nearer pole along the meridian of the place<\/p>\r\n",
"9": "none of these.<\/p>\r\n"
}
},
"question": {
"1": "The stereo plotting instruments are generally manufactured on the principle of<\/p>\r\n",
"2": "Latitude of a place is the angular distance from<\/p>\r\n"
}
};
var newMain = {};
for (var key in main.question) {
newMain[ key ] = [
main.question[key],
main.answer_options[key]
];
}
console.log(newMain);