0

I am looking to return a string if a key isnt avaiable within a map function. This is my current function, where sometimes it would be unable to find the key edBidResponse and return an Error.

const winCpm = () => {
  return data.map((result) => result.response.response.edBidResponse.responseObject.winCpm);
};

When an API is called sometimes .result.response.response. will not have an edBidResponse object. As an example shows...

With

id like to return data.map((result) => result.response.response.edBidResponse....

"response": {
            "response": {
                "defLevel": "xxxx",
                "reason": "yyy",
                "edBidResponse": { ...

Without

Id like to return a string "Empty Value" For each value where edBidResponse was not present.

"response": {
            "response": {
                "defLevel": "xxx",
                "reason": "yyy",
                "logEvent": [...

I have tried many if statements but nothing seems to click, i have read some SO stuff on reduce but never used that. Any tips would be appreciated.

1
  • Why don't you change your map function to simply include a conditional test, returning one thing if result.response.response.edBidResponse is defined and another thing if not? Commented Jul 6, 2021 at 13:35

5 Answers 5

4

Use Optional Chaining for checking if a property has existed or not.

The optional chaining will return undefined in whichever depth it fails to detect a property does not exist. For example, if the result has no response property then it returns undefined except throwing an error.

const winCpm = () => {
  return data.map((result) => result?.response?.response?.edBidResponse?.responseObject?.winCpm || 'Some fallback string');
};
Sign up to request clarification or add additional context in comments.

1 Comment

Genius! Must learn to use those ? more. This is perfect...nice and clean too :)
0

You can do few things. This solution can work for you.

     const winCpm = () => {
      return data.map((result) => result.response.response.edBidResponse ? 
        result.response.response.edBidResponse.responseObject.winCpm : 'Empty value');
    };

Comments

0

You can use the get method from lodash if you have lodash installed.

The first argument is the data, the second argumet is the path and the third is the default value.

const winCpm = () => {
    return data.map((result) =>
      _.get(result, 'response.response.edBidResponse.responseObject.winCpm', '');
    };
    console.log()
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Comments

0

Simply use if and else :

 const winCpm = () =>
      data.map(result => {
        if (result.response.response.edBidResponse)
          return result.response.response.edBidResponse.responseObject.winCpm
        else return 'Empty Value'
      })

Comments

0

Try this code snippet

import _ from "lodash"

const winCpm = () => {
  return (
    <>
      {data.map((result) => {
        let res = result.response.response
        if(_.has(res, 'edBidResponse')){
          ... do stuff accordingly
        }
        else{
          ..other code..
        }

      })}
    </>
  )
};

Comments

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.