2d数组使用下划线的_.map关联数组 [英] 2d array to associative array using underscore's _.map
本文介绍了2d数组使用下划线的_.map关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试映射2d数组:
var fkOptionList2d = [[3,'Orange'],[5 ,'Banana'],[6,'Coconut']]
到一个关联数组: p>
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}]
}));
?
解决方案<根据我的理解,您需要使用
return [{
id:id, label:item
}]
而不是
return [{id:+ id,label:+ item}]
您从函数返回无效的JSON
此外,您不需要 _。对象
方法
fkTableArr1d = _.map(fkTableArr2d,function(item,id){
return [{
id:id,label:item
}];
});
演示
I'm trying to map 2d array:
var fkOptionList2d = [[3, 'Orange'],[5, 'Banana'],[6, 'Coconut']]
to an associative array:
var fkOptionList1d = [{id: 1, label: 'Orange'},{id: 2, label: 'Banana'},{id: 3, label: 'Coconut'}]
but I'm new to underscore.js and don't quite get it yet. Should it be something like:
fkTableArr1d = _.object(_.map(fkTableArr2d, function(item, id) {
return [{"id: " + id,"label: " + item}]
}));
?
解决方案
As per my understanding you need to use
return [{
"id " : id, "label " : item
}]
instead of
return [{"id: " + id,"label: " + item}]
You are returning invalid JSON from the function
Additionally you don't need _.object
method
fkTableArr1d = _.map(fkTableArr2d, function(item, id) {
return [{
"id " : id, "label " : item
}];
});
DEMO
这篇关于2d数组使用下划线的_.map关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文