[英]Accessing the query variable from within callback
请看以下示例:
getOptions() {
let options = {};
if (this.props.location.hasOwnProperty('query')) {
const query = this.props.location.query;
const queriesMap = {
'createdBy': 'createdBy',
'state': 'state',
'created_from': 'created_at[from]',
'created_to': 'created_at[to]'
};
Object.keys(query).map(function(key) {
if(queriesMap.hasOwnProperty(key)) {
options = Object.assign(options, { queriesMap[key]: query[key] });
}
});
}
return options;
}
我正在使用queriesMap
对象来映射URL参数,以建立一个新的URL来调用API。 问题是,当我尝试从.map回调中访问query
时,该query
是未定义的。
如何访问query
变量?
像你看起来是缺少[]
围绕queriesMap[key]
。 因此应该是options = Object.assign(options, { [queriesMap[key]]: query[key] });
。
另外,您可以只执行options[queriesMap[key]] = query[key]
而不是Object.assign
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.