简体   繁体   English

如何使用从 JSON 响应的对象数组中获取值

[英]How can I use get values from array of objects from JSON response

I am trying to learn how to use API's in react.我正在尝试学习如何在反应中使用 API。 I am making a search input for country names using the Rest countires API.我正在使用 Rest 国家 API 搜索国家名称。 I am getting data from https://restcountries.eu/rest/v2/all but I do not know how to handle this data as I can not use map on an object.我从https://restcountries.eu/rest/v2/all获取数据,但我不知道如何处理这些数据,因为我无法在 object 上使用 map。

import axios from "axios";
import React, { useEffect, useState } from "react";

const App = () => {
 const [countries, setCountries] = useState([]);
 const [searchName, setSearchName] = useState("");

  useEffect(() => {
  axios.get("https://restcountries.eu/rest/v2/all").then(response => {
    setCountries(response.data);
   });
  }, []);

  const handleSearch = event => {
  setSearchName(event.target.value);
   };


   return (
  <div>
    <div>
      find countries <input onChange={handleSearch} />
    </div>
   <div></div>
  </div>
  );
 };

 export default App;

Expected to list countries after typing such as: sw = Botswana, Swaziland, Sweden...键入后应列出国家/地区,例如:sw = 博茨瓦纳、斯威士兰、瑞典...

From the question it seems like, these are requirements of your app - 1从问题看来,这些是您的应用程序的要求 - 1

  1. you need to search by country name您需要按国家名称搜索
  2. As you type in, list of countries matching the search should be displayed.当您输入时,应显示与搜索匹配的国家列表。

I created this sandbox with the code you provided - https://codesandbox.io/embed/58115762-rest-countries-o638k .我使用您提供的代码创建了这个沙箱 - https://codesandbox.io/embed/58115762-rest-countries-o638k

It shows a pair of country name and its capital as you enter input in the search box.当您在搜索框中输入输入时,它会显示一对国家名称及其首都。

This is how I changed your code:这就是我更改您的代码的方式:

  1. You need to search countries?您需要搜索国家/地区吗? - Use search API with country name as value of text input - searchName - 使用search API 与国家名称作为文本输入的值 - searchName

https://restcountries.eu/rest/v2/name/ ${searchName} https://restcountries.eu/rest/v2/name/ ${searchName}

  1. To display the output with countries matching your search keyword - map over countries and get appropriate keys.在国家/地区显示 output 以及与您的搜索关键字匹配的countries - map并获取适当的密钥。 Pass those keys as props to your newly created Country component.将这些键作为道具传递给您新创建的Country组件。

    Note, I did not need to change how you handled the JSON response.请注意,我不需要更改您处理 JSON 响应的方式。 The searchName and countries are the only two state variables used to render the UI. searchNamecountries是仅有的两个用于呈现 UI 的 state 变量。

you will need to render countries after fetching from ajax request as like:从 ajax 请求获取后,您需要呈现国家/地区,如下所示:

import axios from "axios";
import React, { useEffect, useState } from "react";

 const App = () => {
 const [countries, setCountries] = useState([]);
 const [searchName, setSearchName] = useState("");

 useEffect(() => {
 axios.get("https://restcountries.eu/rest/v2/all").then(response => {
  setCountries(response.data);
 });
 }, []);

 const handleSearch = event => {
 setSearchName(event.target.value);
 };


 return (
  <div>
  <div>
   find countries <input onChange={handleSearch} />
  </div>
  <ul>
    {(countries.length<=0)?"":
    countries.map(country=> <li>country.name</li> )
        }
  </ul>
 </div>
);
};

export default App;

I think this is what you are looking for.我想这就是你要找的。 If you have got questions, dont hesitate to ask:)如果您有任何问题,请不要犹豫:)

 import axios from "axios"; import React, { useEffect, useState } from "react"; const App = () => { const [countries, setCountries] = useState([]); const [searchName, setSearchName] = useState(""); useEffect(() => { axios.get("https://restcountries.eu/rest/v2/all").then(response => { setCountries(response.data); }); }, []); const handleSearch = event => { let str = event.target.value; let filteredCountries = countries.filter((country) => country.name.toLowerCase().includes(str.toLowerCase())); setCountries(filteredCountries); setSearchName(str); }; return ( <div> <div> find countries <input onChange={handleSearch} /> </div> <ul> {(countries.length <= 0)? "": countries.map(country => <li>{country.name}</li>) } </ul> </div> ); }; export default App;

data =[your array];
countryList = data.map(data=>data.name)
console.log(countryList)

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

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