【问题标题】:Iterate a JSON array by a key value in react-native通过 react-native 中的键值迭代 JSON 数组
【发布时间】:2019-01-09 05:56:04
【问题描述】:

无论如何从 json 数组中获取对象中的值。我需要从一个基于另一个值的对象中获取一个值。

我的代码如下:

export default class StandardComp extends Component {
    constructor(props) {
        super(props)
        this.state = {
           id: '',
           email: 'abc@gmail.com',
           dataSource: []
        };    
    }

    componentDidMount(){
        fetch(someURL, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
           }
        })
        .then((response) => response.json())
        .then((responseJson) => {
            this.setState({dataSource: responseJson})
            //dunno what to do here
        })
        .catch((error) => {
           console.error(error);
        })
    }
}

我的“responseJson”是这样的。然后提供键值(abc@gmail.com),我怎样才能得到字符串“abcdef”?

[
   {
      "id": "qwerty",
      "email": "cat@gmail.com",
      "name": "cat"
   },
   {
      "id": "abcdef",
      "email": "abc@gmail.com",
      "name": "abc"
   }         
   {
      "id": "owowao",
      "email": "dog@gmail.com",
      "name": "dog"
   },
]

提前谢谢你。

【问题讨论】:

    标签: json react-native


    【解决方案1】:

    找到匹配email的元素并返回id。

    array::find

    const data = [
       {
          "id": "qwerty",
          "email": "cat@gmail.com",
          "name": "cat"
       },
       {
          "id": "abcdef",
          "email": "abc@gmail.com",
          "name": "abc"
       },       
       {
          "id": "owowao",
          "email": "dog@gmail.com",
          "name": "dog"
       },
    ];
    
    const findIdByEmail = (data, email) => {
      const el = data.find(el => el.email === email); // Possibly returns `undefined`
      return el && el.id; // so check result is truthy and extract `id`
    }
    
    console.log(findIdByEmail(data, 'cat@gmail.com'));
    console.log(findIdByEmail(data, 'abc@gmail.com'));
    console.log(findIdByEmail(data, 'gibberish'));

    【讨论】:

      【解决方案2】:

      代码将取决于您如何获得值abc@gmail.com

      您可能需要通过道具将其作为参数传递给componentDidMount?或者将其提取到单独的函数中。这取决于。

      这样的事情是我想说的最基本的方式。

      const value = responseJson.filter(obj => obj.email === 'abc@gmail.com')[0].id
      

      这里是在你的类中实现的。

      export default class StandardComp extends Component {
        ...
      
        componentDidMount(){
          fetch(someURL, {
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
             }
          })
          .then((response) => response.json())
          .then((responseJson) => {
              this.setState({ dataSource: responseJson })
              const { email } = this.state
              const value = responseJson.filter(obj => obj.email === email)[0].id
      
          })
          .catch((error) => {
             console.error(error);
          })
        }
      

      }

      【讨论】:

      • 谢谢,我已经在 this.state.email 中存储了值“abc@gmail.com”
      • 仅供参考,如果没有元素匹配过滤器,过滤器将返回一个空数组,因此您不能假设有第零个索引可以返回 id 的。
      • 我赞成您的回答,因为它是一个更好的实现。感谢您帮助我识别这种边缘情况!
      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 2020-03-09
      • 2018-12-06
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      相关资源
      最近更新 更多