简体   繁体   English

如何在 JS 中获取数组而不是 object 的数据?

[英]How to fetch data an array instead of an object in JS?

So im not 100% sure that this is the problem, but Im using the fetch method to grab data from this link https://swapi.py4e.com/api/people .所以我不是 100% 确定这是问题所在,但我使用 fetch 方法从这个链接https://swapi.py4e.com/api/people获取数据。 And then im trying to use the .map function on it.然后我尝试在上面使用.map function 。 Instead of working, I get a Unhandled Rejection (TypeError): jedis.map is not a function error.我没有工作,而是收到Unhandled Rejection (TypeError): jedis.map is not a function错误。

When I console logged jedis I got everything you see in that link, like count: 87, next: " https://swapi.py4e.com/api/people/?page=2 ", etc.当我控制台记录jedis时,我得到了您在该链接中看到的所有内容,例如计数:87,下一个:“ https://swapi.py4e.com/api/people/?page=2 ”等。

Im assuming that the error means that jedis is actually a object and not an array.我假设错误意味着绝地实际上是jedis而不是数组。 But again, im not totally sure thats the reason.但同样,我不完全确定这就是原因。 If it is, how can I access the data so that jedis is an array and not an object如果是,我如何访问数据以使jedis是一个数组而不是 object

import React from 'react';
import './App.css';
import CardList from './CardList'

class App extends React.Component{
  constructor() {
    super();
    this.state = {
      jedis: []
    }
  }

  componentDidMount() {
    fetch('https://swapi.py4e.com/api/people').then(resp => resp.json())
    .then(jedi => this.setState({jedis: jedi}));
  }


  render() {
    return (
      <CardList jedis={this.state.jedis}/>

      )
  }

}

export default App;
import React from 'react'
import Card from './Card'

const CardList = ({ jedis }) => {
    return (
        <div>
            {
                //console.log(jedis)
                jedis.map((jedi, i) => {
                    return (
                        <Card
                            key = {i}
                            name = {jedi.name}
                            height = {jedi.height}
                            mass = {jedi.mass}
                        />
                        )

                })

            } 
        </div>

    )   


}


export default CardList;

Heres the API im using incase anyone is wondering http://swapi.py4e.com/继承人 API 我正在使用以防万一有人想知道http://swapi.py4e.com/

Based on the response of the API,根据 API 的响应,

Change your CardList component props to:将您的CardList组件道具更改为:

<CardList jedis={this.state.jedis.results} />

Also, then you need to change your state initialisation to:此外,您需要将 state 初始化更改为:

this.state = {
  jedis: {
    count: 0,
    previous: null,
    next: null,
    results: []
  }
}

Assuming you want to keep adding to the array on every fetch, you will want to use the spread operator and previousState .假设您想在每次提取时继续添加到数组中,您将需要使用扩展运算符和previousState

fetch('https://swapi.py4e.com/api/people').then(resp => resp.json())
.then(jedi => { 

    this.setState((previousState) => {
      return {
        jedis: [...previousState.jedis, jedi.results],
      };
    });

});

It's quite simple.这很简单。 Actually you need to convert the response object to Array and you will be good to run map on it.实际上,您需要将响应 object 转换为 Array 并且您可以在其上运行map Below is an example assume you get numbers as keys, if your obj response keys are not numbers but regular strings you can replace Number(key) with key in return statement.下面是一个示例,假设您将数字作为键,如果您的 obj 响应键不是数字而是常规字符串,您可以在 return 语句中将Number(key)替换为key

var obj = { "1": 5, "2": 7, "3": 0, "4": 0}; var result = Object.keys(obj).map(function (key) { return [Number(key), obj[key]]; });

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

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