Sho*_*hoe 1 javascript arrays object-literal
我有一个现有的对象文字:
var words=[
{
"word":"callous",
"definition":"uncaring",
"examples": [
{"sentence":"How could you be so callous?"},
{"sentence":"What a callous thing to say!"},
{"sentence":"That showed a callous disregard for the consequences."}
]
}
Run Code Online (Sandbox Code Playgroud)
];
我正在尝试动态添加更多对象,如下所示:
var obj={};
obj.word="nextword";
obj.definition ="nextword definition";
obj.examples= ???;
for (var i = 0; i < nextwordSentencesArray.length; i++) {
??? obj.examples.sentence.push(nextwordSentencesArray[i]);
}
words.push(obj);
Run Code Online (Sandbox Code Playgroud)
我曾在-???-尝试过各种选项,但没有任何效果。感激之至。
为什么不采用完整的对象进行推送以及nextwordSentencesArray具有所需属性的映射?
words.push({
word: "nextword",
definition: "nextword definition",
examples: nextwordSentencesArray.map(sentence => ({ sentence }))
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |