For example 2, having separate Objects to store the data related to pages is a bit misleading and accessing them by the lowercased output of another property could be buggy and not work as expected, what about storing all data in the same place?
const pageName = document.querySelector('#my-div').dataset.pageName;
const dataPageMapper = {
homepage: {
title: 'Home',
layout: 'index'
},
productpage: {
title: 'Product',
layout: 'pdp'
},
blogpage: {
title: 'Blog',
layout: 'blog'
}
};
const currentPage = dataPageMapper[pageName];
// and be careful with accessing properties of an object that doesn't match your pages config
console.log(currentPage.title + '_' + currentPage.layout);
Hi, I am a passionate web developer with a strong background in Computer Science. I have done M.Tech in Computer Science from IIT Kanpur. And currently working as a Principal engineer in MNC.
Actually, I have taken the more general case when the clubbing is not possible i.e it could be the first object and 2nd object are seperate and coming from different files or place.
for this case though, we could have club the data.
Hi, I am a passionate web developer with a strong background in Computer Science. I have done M.Tech in Computer Science from IIT Kanpur. And currently working as a Principal engineer in MNC.
Yes sir, I agree with your comment, what I am trying to correct here is about replacing the switch, think of days of the week as fruits, and you will get my point.
Hi, I am a passionate web developer with a strong background in Computer Science. I have done M.Tech in Computer Science from IIT Kanpur. And currently working as a Principal engineer in MNC.
Hi, I am a passionate web developer with a strong background in Computer Science. I have done M.Tech in Computer Science from IIT Kanpur. And currently working as a Principal engineer in MNC.
Maybe it would be better to use an array for the days example considering that Su-Sa = 0-6.
For example 2, having separate Objects to store the data related to pages is a bit misleading and accessing them by the lowercased output of another property could be buggy and not work as expected, what about storing all data in the same place?
Actually, I have taken the more general case when the clubbing is not possible i.e it could be the first object and 2nd object are seperate and coming from different files or place.
for this case though, we could have club the data.
Thanks for the suggestion!
Yes Victor, As array is also an object in javascript so it would also work the same. Thanks for the suggestion. I'll update the code.
The day of week was just a example....
Yes sir, I agree with your comment, what I am trying to correct here is about replacing the switch, think of days of the week as fruits, and you will get my point.
Yes Sir, an array works well for months, days, etc.
The weekday example is not good...
Updating the code.
@frankwisniewski Above was just an example to illustrate how we can acheive the switch statement through JS object.
Thanks for one liner though!