我有下面的代码,我想使用 undercorejs 来显示 json 文件中的文本。我需要修改该部分,如评论下方所示,如下所示:
我尝试复制它,但它会导致错误并且不显示任何内容。有什么建议么?抱歉,如果这是一个愚蠢的问题,但我是编程新手。
hold.options (tooltips object) = { (property based on the section generated automatically): { (properly based on the itemName generated automatically): { t: item.Texts,
} }
var log = function(contents) {
if (_.isArray(contents)) {
_.each(contents, function(e, i, l) {
log(e);
$('#result');
$('#result').append('</br></br>');
});
} else {
$('#result').append(contents);
}
};
var ty = [{
"Language": "en-GB",
"Section": "Sales&Marketing",
"ItemName": "CalculationType",
"Texts": "Having selected the account heading select the calculation ..."
}, {
"Language": "en-GB",
"Section": "Taxes",
"ItemName": "Save",
"Texts": "The Master Tax Table has been pre populated with the current UK, ..."
}];
var out = [];
_.each(ty, function(item, key) {
var hold = {};
//Part that needs to change
hold.options = {
Taxes: {
Save: {
t: item.Texts,
}
}
};
out.push(hold)
});
_.each(out, function(item) {
log(item.options.Taxes.Save.t + '<br>');
})
最佳答案
这似乎有效:
//Part that needs to change
hold.options = {};
hold.options[item.Section] = {};
hold.options[item.Section][item.ItemName] = {t: item.Texts};
out.push(hold)
});
_.each(out, function(item) {
if(item.options.Sales !== undefined && item.options.Sales.Type !== undefined){
log(item.options.Sales.Type.t);
}
else if(item.options.Taxes !== undefined && item.options.Taxes.Save !== undefined){
log(item.options.Taxes.Save.t);
}
关于javascript - 使用 js 和 underscore.js 显示代码时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903426/