从字面上创建一个复杂的 Javascript 中的对象或提取对象形成一个数组



我目前正在一个旧网站上工作,该网站只处理xslt/xml文件。 我尝试从"简单"的Javascript代码中生成一些xml。

我正在使用一个 json2xml 节点模块,它允许我解析 Json => xml。

在某些情况下,我需要循环"返回"一些对象。

[
{name: 'tagName', attrs: { style: 'display:none;'}, text: 'tagContent'},
{name: 'tagOtherName', attrs: { style: 'display:none;'}, text: 'tagSomeOtherContent'},
.....
//here i want to insert others similar objects with a for loop on another array of Objects with variable content/size
function() {
....some magic function who can help me
}
]

知道我如何迭代地做到这一点吗? 也许是一些递归的东西?或者将我的对象转换为字符串,然后使用 JSON.parse 将其放入我的对象中?

感谢您的帮助!

...

函数结果的解构语法将按照您想要的方式将对象数组解压缩到列表中。

[
{name: 'tagName', attrs: { style: 'display:none;'}, text: 'tagContent'},
{name: 'tagOtherName', attrs: { style: 'display:none;'}, text: 'tagSomeOtherContent'},
...(() => {
// your function to generate your objects
return [
{},
{},
{}
];
})()
]

最新更新