我想参加 NAV.REPORTS.BMAIL.TITLE 。
动态创建翻译代码( NAV.REPORTS.BMAIL.TITLE )。
using System.Diagnostics;
public void executeCmdCommand(string argument)
{
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C {argument}";
process.StartInfo = startInfo;
process.EnableRaisingEvents = true;
process.SynchronizingObject = this;
process.Exited += (sender, e) => {
HideImage();
};
process.Start();
//process.WaitForExit();
}
=>有效let X = this.appGlobal.GetNavigationLanguage().data;
=>有效但是请记住,我已经动态创建了翻译代码,我需要这样的东西:
X.NAV.REPORTS.BMAIL.TITLE
我该如何实现?
答案 0 :(得分:2)
test_data = {
NAV: {
REPORTS: {
BMAIL: {
TITLE: "hello"
}
}
}
}
let transCode = 'NAV.REPORTS.BMAIL.TITLE';
properties = transCode.split('.'); //--> ["NAV","REPORTS","BMAIL","TITLE"]
result = test_data
properties.forEach(function(property) {
result = result[property]
})
console.log(result) // --> hello
答案 1 :(得分:1)
短而邪恶的路线如下:
console.log(eval(`X.${transCode}`));
不太邪恶的方法是使用递归函数调用,这意味着您仅查看字符串路径中的项数(而不是循环整个集合)。
const X = {
NAV: {
REPORTS: {
BMAIL: {
TITLE: 'Test'
}
}
}
}
const transCode = 'NAV.REPORTS.BMAIL.TITLE';
// Evil...
console.log(eval(`X.${transCode}`)); // Test
// Less Evil (but needs exception handling)...
function getData(input: any, splitPath: string[]) {
const level = splitPath.pop();
if (splitPath.length === 0) {
return input[level];
} else {
return getData(input[level], splitPath);
}
}
const result = getData(X, transCode.split('.').reverse());
console.log(result); // Test