我有我需要的JSON对象。javasccript - 创建新对象
const tpl = require('../../../../assets/jsons/article-tpl.json');
然后,我通过遍历数组,我想使用这个对象在每个迭代上创建,因为它是在article-tpl.json
定义一个新的模板。
angular.forEach(content.posts, (val, key) => {
let _tpl = new Object(tpl);
// do some things with _tpl
// push _tpl to array to hold all the different tplts
}
});
此代码不起作用。 我得到一组相同的模板。你能帮我弄清楚是什么问题吗? 谢谢。
请问您可以添加一些更多的代码或提供一个plunker? – varit05
请尝试以下技巧:'let _tpl = JSON.parse(tpl);' –
@AlexM那真的很好。我用'_tpl = JSON.parse(JSON.stringify(tpl));'因为它已经是一个对象而不是一个字符串。最终使用lodash'cloneDeep'。 –