【发布时间】:2018-02-07 16:22:05
【问题描述】:
我正在链接到一个外部 JSON 文件,并希望将其中的两个数组转换为一个数组,以便与 MustacheJS、Handlebars、Dust 等 JS 模板框架一起使用。
这似乎可以在下划线中使用extend,但我发现了另一种称为concat 的可能性,但这似乎只在两个单独的数组而不是一个文件https://www.w3schools.com/jsref/jsref_concat_array.asp 上得到证明
是否可以合并来自单一来源的两个数组以用于 JS 模板?
这是 JSON http://myjson.com/kjs8l
所以本质上,转换:
[
[
{
"productimage": "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto/i0lfddlghaiwfqlvlqay/air-vortex-shoe-fmq6pS.jpg",
"producturl": "https://www.nike.com/gb/t/air-vortex-shoe-fmq6pS"
},
{
"productimage": "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto/cmuof8adhfipkvd0f43r/air-max-95-shoe-XPTbV2mM.jpg",
"producturl": "https://www.nike.com/gb/t/air-max-95-shoe-XPTbV2mM"
}
],
[
{
"sitename": "Nike",
"sitetitle": "Nike. Just Do It.. Nike.com",
"siteurl": "https://www.nike.com/gb/en_gb/"
}
]
]
进入
[
{
"productimage": "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto/i0lfddlghaiwfqlvlqay/air-vortex-shoe-fmq6pS.jpg",
"producturl": "https://www.nike.com/gb/t/air-vortex-shoe-fmq6pS"
},
{
"productimage": "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto/cmuof8adhfipkvd0f43r/air-max-95-shoe-XPTbV2mM.jpg",
"producturl": "https://www.nike.com/gb/t/air-max-95-shoe-XPTbV2mM"
}
{
"sitename": "Nike",
"sitetitle": "Nike. Just Do It.. Nike.com",
"siteurl": "https://www.nike.com/gb/en_gb/"
}
]
【问题讨论】:
-
之后你想要什么?具有给定内容的文件?还是JS中的数组?
-
只需在 MustacheJS 中使用/加载合并数组
标签: javascript arrays json handlebars.js underscore.js