5 回答
TA贡献1877条经验 获得超1个赞
您可以使用Array reduce方法来制作结果对象。
let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
let translatedArray = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"];
let ret = weekdays.reduce((p, c, i) => {
p[c] = translatedArray[i];
return p;
}, {});
console.log(ret);
console.log(ret["Tuesday"]);
TA贡献1786条经验 获得超13个赞
我的简单方法是在这样的对象中维护一个 json
let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
let anotherLanguageDays = {
"Monday" : "星期一",
"Tuesday" : "星期一",
... and so on
};
现在当你遍历数组时你可以做这样的事情
weekdays.forEach(d => {
console.log(anotherLanguageDays[d]);
})
您的代码的完整示例...
let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
let anotherLangauge = {
Monday : "Lundi",
Tuesday : "Mardi",
Wednesday : "Mercredi",
Thursday : "Jeudi",
Friday : "Vendredi"
};
weekdays.forEach(d => {
console.log(anotherLangauge[d]);
})
TA贡献1813条经验 获得超2个赞
使用代表语言代码的键创建一个对象。每种语言代码都有另一个对象,其中包含单词和该语言中单词的翻译。
编写一个函数,该函数接受一个字符串数组作为您要翻译的单词,以及一个指示从哪种语言获取翻译的语言代码。
确保将所有键都小写以保持统一。这些值没有小写。
按照这种方法,您可以通过添加更多语言和单词来轻松扩展。
const translations = {
'en-US': {
monday: 'Monday',
tuesday: 'Tuesday',
wednesday: 'Wednesday',
thursday: 'Thursday',
friday: 'Friday',
saturday: 'Saturday',
sunday: 'Sunday'
},
'fr-FR': {
monday: 'Lundi',
tuesday: 'Mardi',
wednesday: 'Mecredi',
thursday: 'Jeudi',
friday: 'Vendredi',
saturday: 'Samedi',
sunday: 'Dimanche'
},
'nl-NL': {
monday: 'Maandag',
tuesday: 'Dinsdag',
wednesday: 'Woensdag',
thursday: 'Donderdag',
friday: 'Vrijdag',
saturday: 'Zaterdag',
sunday: 'Zondag'
}
};
const translate = (strings, language) => {
if (!translations.hasOwnProperty(language)) {
throw new Error(`Language ${language} does not exist in the translations object.`);
}
const set = translations[language];
return Object.entries(set)
.filter(([ key, value ]) => strings.includes(key))
.map(([ key, value ]) => value);
};
let weekdays = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday'
];
const frenchWeekdays = translate(weekdays, 'fr-FR');
const englishWeekdays = translate(weekdays, 'en-US');
const dutchWeekdays = translate(weekdays, 'nl-NL');
console.log(frenchWeekdays);
console.log(englishWeekdays);
console.log(dutchWeekdays);
// Example with non existent translations.
translate(weekdays, 'de-DE');
TA贡献1812条经验 获得超5个赞
它主要归结为选择最方便的map
ping 方法。一种可能的方法可能类似于下一个提供的示例代码......
const i18nMap = {
"en-GB": {
weekdays: {
monday: "Monday",
tuesday: "Tuesday",
wednesday: "Wednesday",
thursday: "Thursday",
friday: "Friday",
},
},
"fr-FR": {
weekdays: {
monday: "Lundi",
tuesday: "Mardi",
wednesday: "Mercredi",
thursday: "Jeudi",
friday: "Vendredi",
},
},
"de-DE": {
weekdays: {
monday: "Montag",
tuesday: "Dienstag",
wednesday: "Mittwoch",
thursday: "Donnerstag",
friday: "Freitag",
},
},
};
function getItemFromBoundLanguageMap(item) {
return this[item];
}
// let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
const weekdaysKeyList = Object.keys(i18nMap["en-GB"].weekdays);
const weekdaysEnGB = weekdaysKeyList.map(
getItemFromBoundLanguageMap,
i18nMap["en-GB"].weekdays
);
const weekdaysFrFR = weekdaysKeyList.map(
getItemFromBoundLanguageMap,
i18nMap["fr-FR"].weekdays
);
const weekdaysDeDE = weekdaysKeyList.map(
getItemFromBoundLanguageMap,
i18nMap["de-DE"].weekdays
);
console.log("weekdaysKeyList :", weekdaysKeyList);
console.log("weekdaysEnGB :", weekdaysEnGB);
console.log("weekdaysFrFR :", weekdaysFrFR);
console.log("weekdaysDeDE :", weekdaysDeDE);
.as-console-wrapper { min-height: 100%!important; top: 0; }
TA贡献1802条经验 获得超5个赞
您可以执行以下操作:
let arr = ["Mon", "Tue"]
let mapping = {
"Mon":"aaa",
"Tue":"sss"
}
newArr = arr.map((str) => {
return mapping[str];
});
console.log(newArr) //["aaa","sss"]
添加回答
举报