【发布时间】:2019-09-09 21:38:52
【问题描述】:
我有一个要解析的对象。
看起来像这样:
var inputDescriptions = {
customText: {
message: "this is a message",
note: "this is a note"
},
tool: "wrench",
garage: "two-door",
"garage-color": "white",
message: "message",
"message-color": "white",
note: "note"
};
在解析结束时,我希望它是一个包含项目和描述的数组,如下所示:
这被称为“输入描述”
[{
item: "tool",
description: "wrench"
}, {
item: "garage",
description: "two-door"
}, {
item: "garage-color",
description: "white"
}, {
item: "noteText",
description: "this is a note"
}, {
item: "messageText",
description: "this is a message"
}, {
item: "message",
description: "message"
}, {
item: "note",
description: "note"
}]
我真的被一个具有两个属性的对象所抛弃。
这是我拥有的功能:
const parseSelections = (inputDescriptions) => {
return Object.keys(inputDescriptions).map((choiceId) => {
const optionId = inputDescriptions[choiceId];
const userSelectionInput = {
choiceId,
optionId,
};
return userSelectionInput;
});
};
const selections = parseSelections(inputDescriptions);
它接近但不完全在那里:
[
{item: "tool", description: "wrench"}
{item: "garage", description: "two-door"}
{item: "garage-color", description: "white"}
{item: "customText", description: {message: "this is a message", note: "this is a note"}
{item: "message", description: "message"}
{item: "note", description: "note"}
]
我尝试使用 Object.assign 拉出 customText 对象并重新分配,但我仍然无法让它工作。也许我需要创建一个新函数来再次解析?我觉得我离得很近,但物体却把我甩了。有没有人有更好的方法来做到这一点?
【问题讨论】:
标签: javascript arrays json parsing object