Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript合并来自两个不同函数的数组_Javascript_Arrays - Fatal编程技术网

JavaScript合并来自两个不同函数的数组

JavaScript合并来自两个不同函数的数组,javascript,arrays,Javascript,Arrays,嘿,我想合并这两个数组并将其用作一个数组。 array = tempArray1.concat(tempArray2); 分配变量: var worldData = []; var europeData = []; var allData = []; 从数据源1获取数据的代码: fetch('https://disease.sh/v3/covid-19/all') .then((res) => res.json()) .then((data_world) => g

嘿,我想合并这两个数组并将其用作一个数组。

array = tempArray1.concat(tempArray2);
分配变量:

var worldData = [];
var europeData = [];
var allData = [];
从数据源1获取数据的代码:

fetch('https://disease.sh/v3/covid-19/all')
    .then((res) => res.json())
    .then((data_world) => getWorldData(data_world));
第一个功能:

const getAllData = (data_world) => {
  worldData = [
    data_world.todayCases,
    data_world.todayRecovered,
    data_world.todayDeath
  ];
  
  return worldData;
};
const getEuropeData = (data_eu) => {
  europeData = [
    data_eu.todayCases,
    data_eu.todayRecovered,
    data_eu.todayDeath
  ];
  
  return europeData;
};
从数据源2获取数据的代码:

fetch('https://disease.sh/v3/covid-19/continents/Europe?strict=true')
    .then((res) => res.json())
    .then((data_eu) => getEuropeData(data_eu));
第二个功能:

const getAllData = (data_world) => {
  worldData = [
    data_world.todayCases,
    data_world.todayRecovered,
    data_world.todayDeath
  ];
  
  return worldData;
};
const getEuropeData = (data_eu) => {
  europeData = [
    data_eu.todayCases,
    data_eu.todayRecovered,
    data_eu.todayDeath
  ];
  
  return europeData;
};
正在合并阵列(不起作用):

我不能将数组放在这些函数之外,因为我从两个不同的源获取数据,并且我希望将所有数据存储在一个数组中。

array = tempArray1.concat(tempArray2);

根据谷歌的快速搜索。

您可以对结果进行分解,并将对象推到数组顶部

setTimeout
用于防止显示空数组

const
结果=[],
addToResult=({todayCases,todayRecovered,TodayDeathers})=>{
结果:push({今日病例,今日康复,今日死亡});
};
取('https://disease.sh/v3/covid-19/all')
.then(res=>res.json())
。然后(添加至结果);
取('https://disease.sh/v3/covid-19/continents/Europe?strict=true')
.then(res=>res.json())
。然后(添加至结果);
setTimeout(()=>console.log(结果),1000)

.as控制台包装{max height:100%!important;top:0;}
找到了答案,但忘了在那里发布

我的解决方案:

让arr=[];
取('https://disease.sh/v3/covid-19/all')
.然后((res)=>res.json())
。然后((数据)=>{
arr.push(
对象。值(数据)[2],
对象。值(数据)[6]
);
返回arr;
});

你能把你的问题说得更清楚吗?你的问题是什么?对,我想说同样的话,具体来说,你说“我需要在这些函数中使用数组,所以将数组放在函数之外不是一个解决方案”是什么意思请尽可能清楚地说明这一点。为什么您需要两个函数来完成相同的任务?您是否有一些数据和想要的结果?我已经尝试过了。它不起作用。它在什么方面不起作用?你没有得到你期望的结果吗?问题是否可能出在对合并数组执行操作的代码中?数组为空。我认为数组根本不会返回。