setTimeout(() => {
let convertData = resolveData(Data);
console.log(`convertData => `, convertData);
}, 0);
/**
* use while
* https://stackoverflow.com/questions/7793811/convert-javascript-dot-notation-object-to-nested-object
*
* @param {object} obj
* @return {*}
*/
function resolveData(obj: object): object {
let result: object = {};
let keyValue = Object.entries(obj);
for (const [key, value] of keyValue) {
// Split key keys array
const keys: string[] = key.split('.');
// Create sub-objects along key as needed
let target: object = result;
while (keys.length > 1) {
const shiftKey = keys.shift();
let targetValue = (target[shiftKey] = target[shiftKey] || {});
target = targetValue;
// target = target[shiftKey] = target[shiftKey] || {};
}
// Set value at end of path
target[keys[0]] = value;
}
return result;
}
const Data = {
'scheduleView.today.en-US': 'Today',
'scheduleView.today.zh-CN': '當天',
'scheduleView.today.zh-TW': '当天',
'scheduleView.today.ko-KR': '今日',
'scheduleView.today.ja-JP': '오늘',
'scheduleView.tues.en-US': 'Tues.',
'scheduleView.tues.zh-CN': '週二',
'scheduleView.tues.zh-TW': '周二',
'scheduleView.tues.ko-KR': '水曜日',
'scheduleView.tues.ja-JP': '화요일',
'scheduleView.week.fri.en-US': 'fri.',
'scheduleView.week.fri.zh-CN': '五',
'scheduleView.week.fri.zh-TW': '五',
'scheduleView.week.fri.ko-KR': '브랜드',
'scheduleView.week.fri.ja-JP': 'ブランド',
};
文章標籤
全站熱搜
留言列表