简体   繁体   English

如何显示来自返回对象而不是数组的 api 调用的数据

[英]How to display data from an api call that returns an object instead of an array

http://ddragon.leagueoflegends.com/cdn/10.16.1/data/en_US/champion.json http://ddragon.leagueoflegends.com/cdn/10.16.1/data/en_US/champion.json

This is the api im attempting to work with.这是我试图使用的 api。 For some reason when i go to map over it to pull data from it and display the data it says champions.map is not a function.出于某种原因,当我去映射它以从中提取数据并显示数据时,它说 Champions.map 不是一个函数。 (I am guessing it says this because i cant iterate over an object using .map. So i tried using Object.keys(champions) and i still cant really get it to do what i want it to do. (我猜它这么说是因为我无法使用 .map 迭代对象。所以我尝试使用 Object.keys(champions) 但我仍然无法真正让它做我想要它做的事情。

I feel im missing something super easy as its been quite some time since i have made any api calls.我觉得我错过了一些非常简单的东西,因为我已经有一段时间没有进行任何 api 调用了。

Here is my code.这是我的代码。

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import ChampionContainer from "./ChampionContainer";
// import { Link } from 'react-router-dom';


const ChampionList = () => {
    const [ champions, setChampions ] = useState([])
    console.log("This is our state",champions)

    useEffect(() => {
            const url = "http://ddragon.leagueoflegends.com/cdn/10.16.1/data/en_US/champion.json";
            axios.get(url)
            .then(response => {
                setChampions(response.data.data);
            })
            .catch(error => {
                console.log("server Error", error)
            })
    }, [champions]);




    return (
        // Object.keys(a).map(function(keyName, keyIndex) {
        //     // use keyName to get current key's name
        //     // and a[keyName] to get its value
        //   })
        <div>
            {Object.keys(champions).map((key) => {
                return (<div key={key}>{champions[key]}</div>)
            })}
            {/* {champions.map((champion) => (
                <ChampionContainer key={champion.id} champion={champion}/>
            ))} */}
        </div>

    );
}


export default ChampionList;

Thanks in advance for the help.在此先感谢您的帮助。

You are right, you're first attempt at doing .map() on champions wont work as it is not an array, it's an object.你是对的,你第一次尝试在champions上做.map()是行不通的,因为它不是一个数组,它是一个对象。

You're second attempt is closer, though when you do -你的第二次尝试更接近,但当你这样做时 -

champions[key]

this returns one of your champion objects.这将返回您的champion对象之一。 You just need to access the property you want from that object, eg您只需要从该对象访问您想要的属性,例如

champions[key].name - to get the name of the champion eg Aatrox Aatrox champions[key].name - 获得champion的名字,例如Aatrox

You have a infinite loop there.你有一个无限循环。 In useEffect pass champions as a parameter, when it changes you excecute useEffect, but in there you setChampions, then champions changes an useEffect is running again.在 useEffect 中将 Champions 作为参数传递,当它改变时你执行 useEffect,但在那里你 setChampions,然后 Champions 改变了 useEffect 再次运行。 infinite loop as a result.结果无限循环。

You debug your program to make sure that respomse.data.data have the information you need?您调试程序以确保 respomse.data.data 包含您需要的信息?

In setChampions function is better to use setChampions(current => current + newData);在 setChampions 函数中最好使用 setChampions(current => current + newData); this is an object, so you should use spread operator to map onto a new object.这是一个对象,因此您应该使用扩展运算符来映射到一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM