关于if else 和 switch优化的问题

WHMac
  • 225
let url = location.search
var idbox = {}
if (url === '?news') {
  idbox = {id: 43}
} else if (url === '?character') {
  idbox = {id: 39631}
} else if (url === '?hotspot') {
  idbox = {id: 8928}
} else if (url === '?relation') {
  idbox = {id: 7972}
} else if (url === '?manage') {
  idbox = {id: 444}
} else if (url === '?scholarship') {
  idbox = {id: 3380}
}
  • 如何优化以上代码,用map?
回复
阅读 1.1k
2 个回答
李十三
  • 13.3k
✓ 已被采纳
let config = { '?news':{id:43}, '?character':{id: 39631}, ...}
let url = location.search;
let idbox = config[url];
宣传栏