反应将对象推入数组,始终将最后一个数组推入 [英] React push object into array, always pushed last array

查看:52
本文介绍了反应将对象推入数组,始终将最后一个数组推入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是React Js的新手.已经尝试将对象推入数组.

i'm new on React Js things. already tried push object into array.

我已经使用axios获得了结果,但是我无法将其推送到array.结果总是返回最后一个数组F.ex: [0]:{channel:"esl_dota2",stream:null},[1]:{channel:"esl_dota2",stream:null},[2]:{channel:"esl_dota2",stream:null},[3]:{channel:"esl_dota2",stream:null},[4]:{channel:"esl_dota2",stream:null},

I already got the result using axios, but i can't push it to array.the result always return last array, F.ex : [0]: {channel:"esl_dota2", stream:null},[1]: {channel:"esl_dota2", stream:null},[2]: {channel:"esl_dota2", stream:null},[3]: {channel:"esl_dota2", stream:null},[4]: {channel:"esl_dota2", stream:null},

GetStream(){
var channels = ['comster404', 'freecodecamp', 'noobs2ninjas', 'esl_dota2', 'ESL_SC2'];
var temp = [];
var tempObj = {};

channels.map((i) => {
  axios({
    url: this.state.API_URL + i
  })
  .then(yeah => {         
    console.log(i);
    tempObj.channel = i;
    tempObj.stream = yeah.data.stream;      
    temp.push(tempObj);               
  })
  .catch(oops => {
    console.log(oops.status);
  });
});

console.log(temp);}

推荐答案

只需在map方法内声明 var tempObj = {}; .那会帮助你的.

Just declare var tempObj = {}; inside your map method. That will help you.

这篇关于反应将对象推入数组,始终将最后一个数组推入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆