0
var fkTableArr1d = new Array();

fkTableArr1d = _.map(fkTableArr2d, function(list, iterator, context) {          
      return { "id " : list[0], "label " : list[1] };
    });

fkTableArr1d变为: [Object, Object, Object]

1 个对象是Object {id : "5", label : "fuel"}

现在我应该如何获取带有标签的元素的 ID fuel

下面一个不起作用:

console.log(fkTableArr1d['fuel']);
4

3 回答 3

2

看起来您正在寻找findWhere()

查看列表并返回与属性中列出的所有键值对匹配的第一个值。

_.findWhere(fkTableArr1d, {label : "fuel"});

演示:http: //jsfiddle.net/38qUE/1/

于 2013-10-29T11:59:12.997 回答
0
var id = 0;
if(fkTableArr1d.label = 'fuel') id = fkTableArr1d.id;

小提琴

于 2013-10-29T11:49:32.733 回答
0

使用以下任何代码片段:

片段1:

fkTableArr1d =[{id : "5", label : "fuel1"}, {id : "6", label : "fuel"}, {id : "4", label : "fuel2"}];
for(x in fkTableArr1d)
    if(fkTableArr1d[x].label == 'fuel')
        alert(fkTableArr1d[x]['id']);//syntax1 for accessing object member. alerts '6'.

小提琴1

片段2:

fkTableArr1d =[{id : "5", label : "fuel1"}, {id : "6", label : "fuel"}, {id : "4", label : "fuel2"}];
for(x in fkTableArr1d)
    if(fkTableArr1d[x].label == 'fuel')
        alert(fkTableArr1d[x].id);//syntax2 for accessing object member. alerts '6'.

小提琴2

于 2013-10-29T11:54:25.647 回答