New react developer, i am getting an array of objects(identifier:'' and name:'') in console, i want to know is there a way to know what kind of type am i getting (for example are 'name's boolean, string, number...?)
console.log("data",stateToProps?.map((data) => data.name) );
Sometimes at work i get to refactor others code and to add typescript, this would help me alot. any suggestions ?
const stateToProps = useSelector((state) => {
const articles = {
sites: state.articles.sites,
brokers: state.siteArticles.brokers,
};
return articles[props.action];
});
console.log("data", stateToProps);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
For JS types, you can write it like this :
const stateToProps = useSelector((state) => {
const articles = {
sites: state.articles.sites,
brokers: state.siteArticles.brokers,
};
return articles[props.action];
});
function getTypeName(val) {
return {}.toString.call(val).slice(8, -1);
}
console.log(
"data",
mapStateToProps?.map((data) => getTypeName(data.name))
);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You will have to use typeof for each one of the elements
const stateToProps = [{name: "X", value: 2}, {name: undefined, value: 2}, {name: [], value: 2}, {name: true, value: 2}]
console.log("data", stateToProps.map((element) => typeof element.name));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can use javascript's typeof operator: https://www.w3schools.com/js/js_typeof.asp