score:1
Accepted answer
there are multiple issues with your code. you cannot just use ${lang}
. you must surround your string with backticks (`) if you want to use template literals.
to access properties of an object you need a a key i.e. a string which you already have so in this case template literals are not required at all.
when you access an array by index you need to use []
not ()
so use delmethod[i]
instead of delmethod(i)
. additionally make sure an property exists on an javascript object.
const delmethod = ["email", "sms", "fax"];
const alertdetails = {
alertmessage: {
en: {
all: "emailsfsdfsdfsd",
fax: "placeholder",
sms: "sdkjföskjfsödkj"
},
},
};
const lang = "en";
for (let i = 0; i < delmethod.length; i++) {
if (
alertdetails &&
alertdetails.alertmessage &&
// use backticks ` when trying to use template literals
alertdetails.alertmessage[`${lang}`] &&
// there is actually no need for template literals here
alertdetails.alertmessage[lang].all &&
// you need to make sure "sms" or "fax" or "email" key actually exist on the object
alertdetails.alertmessage[lang][delmethod[i]] &&
alertdetails.alertmessage[lang][delmethod[i]].includes("placeholder")
) {
console.log(
`alertdetails.alertmessage[${lang}][${delmethod[i]}] does have a placeholder`
);
console.log(true);
} else {
console.log(
`alertdetails.alertmessage[${lang}] does not have property ${delmethod[i]} or does not have a placeholder`
);
console.log(false);
}
}
Source: stackoverflow.com
Related Query
- Accessing dynamic properties of an Object in React
- React accessing object properties and displaying them to the screen
- React router - dynamic routes - accessing state object via the router
- Accessing key name in key-value pairs on props object in React
- Accessing object in returned promise using fetch w/ react js
- React PropType check dynamic object
- react count object properties in an array
- accessing object attributes in the react render method
- Render Object properties in React
- can't access object's properties within object in react
- Dynamic object key with Typescript in React event handler
- Can't access nested object properties in a single object from JSON call in REACT
- React custom hooks taking dynamic data properties
- Mapping object keys in react and returning child properties
- React - Display all properties of an object
- waiting for useeffect to finish and then accessing object properties
- Accessing the whole state object in React 16.8
- React - convert object into array of objects with properties
- React router accessing route properties in route component
- Returning null when accessing object properties in state?
- React Component iterate/loop properties of Mongo object
- How do I get api object properties in react
- Handling dynamic text inputs with one onChange handler and no name properties in React JS
- How to remove a single object from Array Properties in React Js?
- Accessing the "this" keyword in a React state object
- How to Give Dynamic Routing in react js using simple array of object file
- Having difficulty iterating through an Object of Arrays and generating dynamic React elements in JSX from within the Redux Store
- Acessing properties of an object in an array in React
- onChange handler for Object.keys().map in React to update object properties
- Type Error 'Can't read properties of undefined' - Accessing API in React
More Query from same tag
- Sharing Code between Multiple React Applications with Symlinks
- How to hide react bootstrap modal and also call the save function?
- My react input type == number is not working
- How to make display: none at onClick in React in simplest way possible - if any?
- Returning nightmare with .map() inside of a .map()
- How to wait for a useState update within onSubmit?
- How to get React Router to recognize a new route on a dynamic path
- My GIFs are not getting displayed in child component
- star rating value from React to Mongodb
- staggerChildren with framer motion
- npm run build not including all the files
- How to share props to a different route using react-router-dom?
- Can we wrap a HOC around another HOC in React
- How to pass props to {this.props.children}
- React HOC can't pass props to enhanced component
- DetailList fabric ui sorting issue React hooks
- React Components to SvelteKit - Objects with HTML in attribute (JSX rendering) to Script tag
- What is the best way to deal with a fetch error in react redux?
- Update React Hooks State During Render Using useMemo
- React Redux- Timeout before Store Update
- Is the definition of side effects in React the same as in functional programming?
- React call bootstrap function?
- Show Date/time in x axis using react d3
- FAST_REFRESH=FALSE in .env file is not working (React)
- D3 component does not re-draw after receiving new props
- Illegal import declaration with gulp-react
- How do I delay animate, not whileOver or WhileTap in framer motion?
- Implementing table support in draft js
- Microsoft office-ui-fabric-react vs fluent-ui-react - which to choose?
- cant update quantity of a product in a shopping cart using react-redux