新的react开发人员,我正在获得一个对象数组(标识符:"和名称:")在控制台中,我想知道是否有一种方法可以知道我得到什么样的类型(例如'name'的布尔值,字符串,数字…)
console.log("data",stateToProps?.map((data) => data.name) );
const stateToProps = useSelector((state) => {
const articles = {
sites: state.articles.sites,
brokers: state.siteArticles.brokers,
};
return articles[props.action];
});
console.log("data", stateToProps);
对于JS类型,你可以这样写:
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))
);
您必须为每个元素使用typeof
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));
你可以使用javascript的typeof操作符:https://www.w3schools.com/js/js_typeof.asp