我正在尝试映射2d数组:
var fkOptionList2d = [[3, 'Orange'],[5, 'Banana'],[6, 'Coconut']]
到关联数组:
var fkOptionList1d = [{id: 1, label: 'Orange'},{id: 2, label: 'Banana'},{id: 3, label: 'Coconut'}]
但是我刚接触到underscore.js并且还没有得到它。应该是这样的:
fkTableArr1d = _.object(_.map(fkTableArr2d, function(item, id) {
return [{"id: " + id,"label: " + item}]
}));
答案 0 :(得分:1)
根据我的理解,你需要使用
return [{
"id " : id, "label " : item
}]
而不是
return [{"id: " + id,"label: " + item}]
您正在从函数
返回无效的JSON此外,您不需要_.object
方法
fkTableArr1d = _.map(fkTableArr2d, function(item, id) {
return [{
"id " : id, "label " : item
}];
});