1

Is there anyway to get a value in an object from a json array. I need to get a value from an object based on another value.

I have my code like:

export default class StandardComp extends Component {
    constructor(props) {
        super(props)
        this.state = {
           id: '',
           email: '[email protected]',
           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);
        })
    }
}

My "responseJson" is something like this. Then providing the key value ([email protected]), how could I get the string "abcdef"?

[
   {
      "id": "qwerty",
      "email": "[email protected]",
      "name": "cat"
   },
   {
      "id": "abcdef",
      "email": "[email protected]",
      "name": "abc"
   }         
   {
      "id": "owowao",
      "email": "[email protected]",
      "name": "dog"
   },
]

Thank you in advance.

2 Answers 2

6

Find the element that matches email and return the id.

array::find

const data = [
   {
      "id": "qwerty",
      "email": "[email protected]",
      "name": "cat"
   },
   {
      "id": "abcdef",
      "email": "[email protected]",
      "name": "abc"
   },       
   {
      "id": "owowao",
      "email": "[email protected]",
      "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, '[email protected]'));
console.log(findIdByEmail(data, '[email protected]'));
console.log(findIdByEmail(data, 'gibberish'));

Sign up to request clarification or add additional context in comments.

Comments

1

The code will depend on how you get the value [email protected].

You'll probably need to pass it in as an argument to componentDidMount via a prop? Or extract it to a separate function. It just depends.

Something like this is the most basic way I'd say.

const value = responseJson.filter(obj => obj.email === '[email protected]')[0].id

Here it is implemented in your class.

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);
    })
  }

}

2 Comments

Thank you, I already have the value "[email protected]" stored in this.state.email
FYI, filter returns an empty array if no elements match filter, so you can't assume there's a zeroth index to return id of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.