Take the following example:
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;
}
I'm using the queriesMap
object to map url parameters to build a new url to call an API. The problem is that query
is undefined when I'm trying to access it from within the .map callback.
How do I access the query
variable?
Looks like you are missing a []
around queriesMap[key]
. So it should be options = Object.assign(options, { [queriesMap[key]]: query[key] });
.
Also, you could just do options[queriesMap[key]] = query[key]
rather than Object.assign
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.