score:1
Accepted answer
As commenters have indicated, modern JavaScript provides the optional chaining operator (?.
), which you can use like this (as long as you're not still trying to support IE):
const
grandparent = getObject(),
itemsCount = grandparent?.parent?.child?.prop3?.length,
oops = grandparent?.parent?.child?.length;
console.log(itemsCount ?? "No such property");
console.log(oops ?? "No such property");
function getObject(){
return {
parent: {
child: {
prop1: 'alpha',
prop2: 'beta',
prop3: [
{ item: { name: 'first', type: 'string' }},
{ item: { name: 'second', type: 'number' }}
]
}
}
};
}
Source: stackoverflow.com
Related Query
- Is there a cleaner way to check if a deep nested node exists?
- Is there a way to check if an image exists before setting the image source in React when the image source has concatenated variables?
- Is there a better way to check if a variable exists in Javascript?
- Is there a way to check nested resources in react-admin's matchSuggestion?
- Is there a better way to write this check to see if a value exists on a prop in react?
- Is there a way to check if the react component is unmounted?
- Is there a cleaner way of getting current URL in both client and server side in isomorphic react apps?
- Is there a way to check if a lazy-loaded component (with React.Lazy) has finished loading?
- Is there a way to show a Material-UI Drawer nested inside a Grid component?
- Is there a way to control where a nested Material UI select in a popper gets mounted in the DOM?
- Is there a way to make optional React props types cleaner with Typescript?
- Best way to check if there is already a token in local storage using use effect in React Context
- Is there a way to disable the nested field name feature in react final form?
- Is there a way to put every configuration file to a config directory rather than root in Node project?
- Easy way to check if array of objects exists in array of objects before setting state React JS
- Is there any way to check if constructor prop changed and then re-render component in React?
- Is there any way to check prop functions and pass it to the next level
- Is there cleaner way to define React Hooks useState on the top level?
- Is there a way to destructure nested objects by grouping
- Is there a way of passing state information from react hooks into nested functions in react?
- Is there a way to use the nested route ID as a prop argument in React Router
- Best way to ensure nested object value exists before calling it in Javascript
- Check if value exists in nested array
- Firebase - Best way to safely check if and value exists
- Is there a way to check if React's `StrictMode` is on?
- Is there a way to change the url of audioplayer once the state is changed on the component it exists in?
- Is there a way to target a nested JSS styled component within another JSS styled component? [JSS-nested/Styled-JSS/Material UI (React)]
- react native: There is way to fix the check boxes in my example?
- is there a simple way to create a nested dropdown in ReactJS without crazy amounts of state?
- How to propagate state to the root of nested tree from a deep node without using Flux?
More Query from same tag
- Map array to return components with different classNames
- How to copy Redux store in component state with hooks?
- React Router Error Cannot read property 'pathname' of undefined
- How to get title of Tab when tab is changed using onChange in Ant design
- Correct way to use socket.on in react
- Filter json data when click button in react
- My component disappeared from my React Application
- I'm trying to convert a base64 encoded string to a image on my react file
- search results from two API's
- How to include scss compilation to css during CRA build
- React Nat Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application
- type 'string | null' is not assignable to parameter of type 'SetStateAction<string>'.Type 'null' is not assignable to type 'SetStateAction<string>'
- React Router not loading any pages
- Is there a way to stream video in react-native?
- Passing second argument (ownProps) to mapDispatchToProps - is a bad pactice?
- How to import a function in Next.js only on client side?
- What is the best way to handle enum prop in styled-components
- ReactJS Json-loader working in app but not storybook
- How can I do to disable geolocalization?
- localStorage not defined using axios interceptor in NextJS
- Configure gulp task to move files to subfolder
- Next.js project build error: Entry point for implicit type library 'build'
- how to call function inside reactjs render method properly?
- How do I add a conditional class in React without removing other classes?
- ReactTransitionGroup Lower Level API
- Why my custom hook causes infinite data refetching?
- React MUI datatable date range filter
- React textarea is showing [object Object]
- search in the table is not working when search data is made blank in react
- Rendering components inside Route React