我想将对象数组中的前 6 个元素推送到我新制作的数组中。我在网络浏览器上把它弄出来,但是或者“qwe”“qweqweqwe”我得到了 NaN。为什么?假设来自 api 的对象数组如下所示:0: {id: 340, name: "qwe", lastname: "qweqweqweq", …}1: {id: 341, name: "qwe", lastname: "qweqweqweq", …}2: {id: 342, name: "qwe", lastname: "qweqweqweq", …}3: {id: 343, name: "qwe", lastname: "qweqweqweq", …}4: {id: 344, name: "qwe", lastname: "qweqweqweq", …}5: {id: 345, name: "qwe", lastname: "qweqweqweq", …}6: {id: 346, name: "qwe", lastname: "qweqweqweq", …}7: {id: 347, name: "qwe", lastname: "qweqweqweq", …}8: {id: 348, name: "qwe", lastname: "qweqweqweq", …}我的代码如下所示:.then(response => { console.log(typeof response.data, response.data); this.model = { name: response.data.name, lastname: response.data.lastname } this.userInfo.unshift(this.model.name + this.model.lastname); // Here's where i have problem if(this.userInfo.length > 6) this.userInfo.pop(); }) .catch(error => { console.log('Something went wrong', error); }); },
3 回答
30秒到达战场
TA贡献1828条经验 获得超6个赞
如果我理解你的话,你想将前 6 个元素从responseto 推送到userInfo.
如果这是你想要的,你可以这样尝试:
.then(response => {
response.data.some((e, index) => {
this.userInfo.push(e.name + e.lastname)
return index > 5
})
})
RISEBY
TA贡献1856条经验 获得超5个赞
如果您只想移动数组对象的前六个条目,则可以使用此方法
let newArray = response.data.slice(0, 6); // the newArray will have the first six objects of the response data.
添加回答
举报
0/150
提交
取消