を内側のネストされたオブジェクトを拡張する方法を、私は配列にネストされたオブジェクトを拡張するために変換したい、follwingなどの詳細:のNode.jsで
{
"topic":"myTopic",
"content":{
"name": {
"tom1" : {
"value": "String",
},
"tom2" : {
"value": "String",
},
"tom3" : {
"value": "String",
}
}
}
}
変換を、以下の形式に
を拡張var result = []; // contains the array you're looking for
var test = { "topic":"myTopic", "content":{ "name": { "tom1" : { "value": "String", }, "tom2" : { "value": "String", }, "tom3" : { "value": "String", } } } };
Object.keys(test.content.name).map((e,i) => {
let temp = {};
temp[e] = test.content.name[e];
result.push({topic: test.topic, content: { name: temp }});
});
このヘルプあなたはい:あなたのような何かを試すことができ
[{
"topic":"myTopic",
"content":{
"name": {
"tom1" : {
"value": "String",
}
}
}
},
{
"topic":"myTopic",
"content":{
"name": {
"tom2" : {
"value": "String",
}
}
}
},
{
"topic":"myTopic",
"content":{
"name": {
"tom3" : {
"value": "String",
}
}
}
}]
わかりました。あなたの質問は何ですか? –
私はオブジェクトの内部に複数の名前を持っていますが、新しい配列を拡張して新しい配列を生成する良い方法はありますか?それぞれが1つの名前を持っています – thomas
なぜ 'topic'と' content'プロパティは同じオブジェクトにありますか?どのルールによって、オブジェクトを分割するかどうかを決定しますか? – trincot