我试图将包含对象的两个数组连接到一个包含所有对象的数组中,我希望它有意义
getEntries() {
const linksArr = ['/api/aggregated', '/api/techmetro'];
axios.all(linksArr.map(l => axios.get(l))).then(axios.spread((...res) => {
// all requests are now complete
this.articles = res;
}));
},
我确实
articles:Array[2]
0:Object
config:Object
data:Object
data:Array[10]
0: Object
...
meta:Object
headers:Object
request:XMLHttpRequest
status:200
statusText:"OK"
1:Object
config:Object
data:Object
data:Array[1]
0: Object
0:Object
meta:Object
headers:Object
request:XMLHttpRequest
status:200
statusText:"OK"
但我正在寻找的是:
articles:Array[11]
0: Object
...
我错过了什么?非常感谢
答案 0 :(得分:2)
我认为你正在寻找
….then(([aggregateds, techmetros]) => {
this.articles = aggreateds.concat(techmetros);
});
或等效的
….then(res => {
this.articles = res[0].concat(res[1]);
});
如果可以连接多个数组(例如res.length != 2
),则可以使用[].concat(...res)
。
答案 1 :(得分:2)
我认为最简单的方法是从响应中获取数据,例如
... .then( allresponses =>
allresponses.reduce( (current, item) => current.concat( item.data ), []) ...