js map物件處理if
阿新 • • 發佈:2020-08-02
onButtonClick只有一個引數時候,map和object物件都可以
// onButtonClick1(3) onButtonClick只有一個引數時候,map和object物件都可以 const map1 = new Map() .set(1, { message: 'processing', page: 'indexPage' }) .set(2, { message: 'fail', page: 'failPage' }) .set(3, { message: 'success', page: 'successPage' }) .set(4, { message: 'cancel', page: 'cancelPage' }) .set('default', { message: 'other', page: 'otherPage' }) const onButtonClick2 = status => { if (map1.has(status)) { let { message, page } = map1.get(status) sendLog(message) jumpTo(page) } } const sendLog = message => { console.log(message); } const jumpTo = page => { console.log(page); }
增加onButtonClick引數 ,這個時候map是最好的選擇
// 增加onButtonClick引數 ,這個時候map是最好的選擇 const map3 = new Map() .set({ identity: 'guest', status: 1 }, { message: 'processing', page: 'indexPage' }) .set({ identity: 'guest', status: 2 }, { message: 'fail', page: 'failPage' }) .set({ identity: 'guest', status: 3 }, { message: 'success', page: 'successPage' }) .set({ identity:'guest', status: 4 }, { message: 'cancel', page: 'cancelPage' }) .set({ identity: 'guest', status: 5 }, { message: 'other', page: 'otherPage' }) .set({ identity: 'master', status: 1 }, { message: 'processing', page: 'indexPage' }) .set({ identity: 'master', status: 2 }, { message: 'fail', page: 'failPage' }) .set({ identity: 'master', status: 3 }, { message: 'success', page: 'successPage' }) .set({ identity: 'master', status: 4 }, { message: 'cancel', page: 'cancelPage' }) .set({ identity: 'master', status: 5 }, { message: 'other', page: 'otherPage' }) const onButtonClick4 = (identity, status) => { const mapItem = [...map3].find(([key, value]) => { return key.identity === identity && key.status === status }) if (!mapItem) return const [key, { message, page }] = mapItem sendLog(message) jumpTo(page) } onButtonClick4('master', 3)