不确定是否有lodash或其他util方法来映射对象序列。在这种情况下,必须手动重新排序一堆嵌套对象的顺序。
//unprocessed
{
"order": [
"home",
"work"
],
"questions": {
"US": {
"work": "working",
"home": "driving home"
},
"UK": {
"work": "go to work",
"home": "go to home"
}
}
}
如何基于订单的值映射问题对象的对象的答案。处理后的输出应该是
{
"questions": {
"US": {
"home": "driving home",
"work": "working"
},
"UK": {
"home": "go to home",
"work": "go to work"
}
}
}
答案 0 :(得分:0)
“问题”:{ “美国”:{ “ work”:“工作”, “ home”:“开车回家” }, “英国”:{ “ work”:“上班”, “ home”:“回家” } } 是同样的 “问题”:{ “美国”:{ “ home”:“开车回家”, “工作”:“工作” }, “英国”:{ “ home”:“回家”, “工作”:“上班” } } 所以你什么都不做
答案 1 :(得分:0)
如果您实际上要按顺序订购它,那么我希望您使用数组来存储住所和工作。
例如:
问题:{美国:['驾车回家','工作']}
答案 2 :(得分:0)
我不明白为什么要订购键值对。但是我已经完成了一次打印。请参见下面的代码。
var data = { name:'stack',
age:40,
grant:6000
};
console.log(Object.entries(data).sort(function(a, b) {<SORT LOGIC>}).reduce( (o,[k,v]) => (o[k]=v,o), {} ));
答案 3 :(得分:0)
这里有一些代码可以按order
数组中指定的顺序访问它们以显示它们。
const data = {"order": ["home","work"],"questions": {"US": {"work": "working","home": "driving home"},"UK": {"work": "go to work","home": "go to home"}}};
// using/accessing the data to display to client
const {order:orderedAccess, questions} = data;
Object.keys(questions).map(country=>
orderedAccess.forEach(action=>console.log(country, action, questions[country][action]))
)