0

I have the below code which executes all keys and values of the nested JSON. Its working fine but whenever the value is null for any key, the code breaks at reduce function. I would like to display the null value as well.I tried to handle null and undefined scenario in else if but that is not working. Below is the code I have written.It must be a minor change which I am missing out.

  const myObj = {
      "key1": "value1",
      "key2": null,
      "key3": "value3",
      "key4": {
        "k1": "val1",
        "k2": {
          "p1": "v1",
          "p2": "v2",
          "p3": "v3"
        }
      }
    }


class Details extends React.component{
constructor(){
  this.getValues = this.getValues.bind(this);
}
getValues() {
     return (
        <div key={k} className="row">
            <div className="col-xs-6">{k}</div>
            <div className="col-xs-6">{v}</div>
        </div>
    )
    }

generateData(myObj) {

        const newData = Object.keys(myObj).reduce((result, currentKey) => {
          if (typeof myObj[currentKey] === 'string' || myObj[currentKey] instanceof String) {
            const elementToPush = getValues(currentKey, myObj[currentKey]);
            result.push(elementToPush);
          } else {
            const nested = generateData(myObj[currentKey]);
            result.push(...nested);
          }
          return result;
        }, []);
        return newData;
      }

render() {
    return (
        <div>
            <Header />
            <div className="content">
                {this.generateData(myObj)}
            </div>
        </div>
    )
}
}
4
  • Can you give us the error message or stack? Commented Jan 11, 2018 at 6:36
  • It says - "Cannot convert undefined or null to object" Commented Jan 11, 2018 at 6:40
  • More specifically, it says so at Object.keys(...), because you are calling generateData with null. Commented Jan 11, 2018 at 6:40
  • when the value is null it throws this error at Object.keys(myObj).reduce() line in generateData() Commented Jan 11, 2018 at 6:41

1 Answer 1

1

Is this what you are expecting?

 const newData = Object.keys(myObj).reduce((result, currentKey) => {
          if (typeof myObj[currentKey] === 'string' || myObj[currentKey] instanceof String) {
            const elementToPush = getValues(currentKey, myObj[currentKey]);
            result.push(elementToPush);
          } else if(!myObj[currentKey]) {
            const nullElementToPush = getValues(currentKey, "null");
            result.push(nullElementToPush);
          } else {
            const nested = generateData(myObj[currentKey]);
            result.push(...nested);
          }
          return result;
}, []);
Sign up to request clarification or add additional context in comments.

5 Comments

This does not work. It's breaking the key characters and displaying it along with the index.
do you wish to avoid the null from displaying, means skip those keys which is null
I actually want to display null as a value.
In the above code it will check !myObj[currentKey], means null or undefined,you can also use (myObj[currentKey] === null || myObj[currentKey] === undefined), then it will use your getValues function to render the key and the value. If you wish to render the null as empty. then you can give "" instead of "null".
Yeah,i did the same thing.Its working now. Thank you for the solution.Much appreciated.

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.