如下所示,我正在尝试使用undercorejs中的-.each函数来分配json文件中每条记录的值并将其打印到屏幕上..我有这段代码,但它不起作用......任何想法为什么?
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, ..."}
];
arr= {};
_.each(ty, function(val,key) {
arr[key] = val;
});
log(arr);
答案 0 :(得分:2)
我认为CDN的问题。如果您需要语言,则需要指定val.Language
。并且数组定义也是错误的。给new Array
或[]
而不是{}
。请尝试以下方法:
function log(contents) {
var result='';
if(_.isArray(contents)) {
_.each(contents, function(e, i, l) {
//log(e);
result += e.Language+'-'+e.Section+'-'+e.ItemName+'-'+e.Texts+'</br>';
});
}
return result;
};
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, ..."}
];
arr= new Array;
_.each(ty, function(val,key) {
arr[key] = val;
});
var res=log(arr);
$('#result').append(res);
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
&#13;