Arrays 如何将映射项追加到新数组中
我有一些Arrays 如何将映射项追加到新数组中,arrays,reactjs,object,Arrays,Reactjs,Object,我有一些对象s,它们是我从map()一个APIURL得到的,然后fetch()那些URLs,并将这些对象作为return(见图) 因此,我想将所有这些对象放入一个新的数组,这样我就可以使用数组创建一个反应组件,该组件将使用其中的信息 有谁能帮我弄清楚,给我一些关于如何解决这个问题的提示吗?谢谢你 通过在map函数中返回数据,然后将其推入数组,可以创建所需的对象数组 const url = "https://pokeapi.co/api/v2/pokemon?offset=0&
对象
s,它们是我从map()
一个API
URL
得到的,然后fetch()
那些URL
s,并将这些对象作为return
(见图)
因此,我想将所有这些对象
放入一个新的数组
,这样我就可以使用数组
创建一个反应组件
,该组件将使用其中的信息
有谁能帮我弄清楚,给我一些关于如何解决这个问题的提示吗?谢谢你
通过在map函数中返回数据,然后将其推入数组,可以创建所需的对象数组
const url = "https://pokeapi.co/api/v2/pokemon?offset=0&limit=5";
const AppProvider = ({ children }) => {
const fetchUrl = async () => {
const resp = await fetch(url);
const respData = await resp.json();
const data = respData.results;
const newArray = [];
data.map((pokemon) => {
newArray.push(fetchPokemon(pokemon));
});
};
const fetchPokemon = async (pokemon) => {
let url = pokemon.url;
let data = {}
return fetch(url)
.then((resp) => resp.json())
.then((pokemonData) => {
return pokemonData;
});
};
};
如果没有源代码,就不清楚您在说什么。我不太明白,如何使用
constnewarray=[]代码>在本地作用域上?即使我将其移动到全局,也只返回[]
值
const url = "https://pokeapi.co/api/v2/pokemon?offset=0&limit=5";
const AppProvider = ({ children }) => {
const fetchUrl = async () => {
const resp = await fetch(url);
const respData = await resp.json();
const data = respData.results;
const newArray = [];
data.map((pokemon) => {
newArray.push(fetchPokemon(pokemon));
});
};
const fetchPokemon = async (pokemon) => {
let url = pokemon.url;
let data = {}
return fetch(url)
.then((resp) => resp.json())
.then((pokemonData) => {
return pokemonData;
});
};
};