Handling Mixed Type JSON Arrays in JavaScript - TypeError guide
I'm working on a personal project and I'm performance testing and I'm working with a `TypeError: want to read properties of undefined (reading 'name')` when trying to process a JSON array that contains mixed types in JavaScript. The JSON data I'm working with is structured like this: ```json [ { "name": "Alice", "age": 30 }, null, { "name": "Bob", "age": 25 }, { "age": 22 } ] ``` As you can see, this array includes both objects and a `null` value, along with an object that does not contain the `name` property. I am trying to loop through this array and log the names of each person: ```javascript const jsonData = [ { "name": "Alice", "age": 30 }, null, { "name": "Bob", "age": 25 }, { "age": 22 } ]; jsonData.forEach(person => { console.log(person.name); }); ``` When running this code, I get the `TypeError` for the `null` entry and for the object that lacks the `name` property. I've tried using optional chaining like this: ```javascript console.log(person?.name); ``` However, this doesn't solve the question because I still get `undefined` printed for the `null` entry, and the `TypeError` continues. I would like to know how to safely handle mixed types in this array, specifically how to avoid errors and still log only valid names. Is there a more robust way to filter or process this array? Any best practices or patterns would be appreciated, especially for ensuring that my application doesn't crash due to unexpected input in JSON data. I'm on Ubuntu 22.04 using the latest version of Javascript. Thanks for any help you can provide! This issue appeared after updating to Javascript stable. I'm working in a macOS environment.