【发布时间】:2019-04-17 05:37:14
【问题描述】:
我有一个包含 3 个数组的固定 JSON 数组。获取 JSON 后,我试图将它们合并到一个数组中。这是我尝试过的,但 Vue.JS 数组似乎是空的。
以前的
this.items = items;
新尝试
this.items = items.concat.apply([], arrays);
我在下面的链接中放了一个 3 页演示的示例:
<body>
<!-- Page List -->
<div class="container text-center mt-5" id="app">
<h1 class="display-4">Vue Page Output:</h1>
<!--<h2>{{items[0][0].page1}}</h2>-->
<h2>{{items.page1}}</h2>
</div>
<div class="container text-center mt-5">
<h3>Other Pages</h3>
<a href="products.html">Products</a>
<a href="contactus.html">Contact Us</a>
</div>
<!-- /.container -->
<script type="text/javascript">
const app = new Vue({
el: '#app',
data: {
items: []
},
created: function () {
fetch('test.json')
.then(resp => resp.json())
.then(items => {
this.items = items.concat.apply([], arrays);
})
}
});
</script>
</body>
JSON
[
[
{
"page1": "Company Name"
}
],
[
{
"products": "Product List"
}
],
[
{
"contactus": "Contact Us at Acme Corp"
}
]
]
所需的 JSON 输出
JSON
[
{
"page1": "Company Name"
},
{
"products": "Product List"
},
{
"contactus": "Contact Us at Acme Corp"
}
]
【问题讨论】:
-
所以你需要将子数组合并成一个数组,你能发布你想要的输出数组吗?
-
您在arraydemo.netlify.com 的示例错误为
ReferenceError : arrays is not defined。 -
@DILEEPTHOMAS 我已更新问题以反映所需的 JSON 输出
标签: javascript arrays vue.js vuejs2 vue-component