mrpbennett
mrpbennett

Reputation: 1966

Checking for keys in a map function

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.

Upvotes: 0

Views: 128

Answers (5)

Lohitha Y
Lohitha Y

Reputation: 1

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..
        }

      })}
    </>
  )
};

Upvotes: 0

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

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

Upvotes: 4

Soufiane Boutahlil
Soufiane Boutahlil

Reputation: 2604

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

Upvotes: 0

arunmmanoharan
arunmmanoharan

Reputation: 2675

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>

Upvotes: 0

Tigran Petrosyan
Tigran Petrosyan

Reputation: 570

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

Upvotes: 0

Related Questions