Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.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 反应->;仅在一种状态下保存';id';和';名称';物体的形状_Javascript_Reactjs - Fatal编程技术网

Javascript 反应->;仅在一种状态下保存';id';和';名称';物体的形状

Javascript 反应->;仅在一种状态下保存';id';和';名称';物体的形状,javascript,reactjs,Javascript,Reactjs,晚上好,我有一个来自API的objet数组,我只想将对象的“id”和“name”保持在一种状态。有人知道怎么做吗 state = { countries: [//{id: name}, {id: name}, {id: name}] }; componentDidMount() { api.get('/countries').then(res => { // ?? }) } 假设来自API的响应是一个对象数组,您可以简单地在响应中循环以仅提取所需的

晚上好,我有一个来自API的objet数组,我只想将对象的“id”和“name”保持在一种状态。有人知道怎么做吗

state = {
    countries: [//{id: name}, {id: name}, {id: name}]
};
componentDidMount() {
    api.get('/countries').then(res => {
        // ??
    })
}

假设来自API的响应是一个对象数组,您可以简单地在响应中循环以仅提取所需的字段(
id
name
),并使用新数据更新组件的状态

componentDidMount() {
  api.get('/countries').then(res => {
    const countries = res.map((country) => ({
      id, name,
    }));
    this.setState({
      countries
    });
  });
}