角度对象数组到列数组

时间:2014-04-24 11:36:24

标签: javascript arrays linq angularjs

我有对象,其中包含字段idtext以及可能更多其他字段。

数组中有很多对象,我想将其转换为文本数组。

这就是我现在所拥有的。任何更漂亮的解决方案,可能是LINQ之类的东西?

var emails = new Array();
angular.forEach(this.form.Emails, function (email) {
    emails.push(email.text);
});

1 个答案:

答案 0 :(得分:1)

就像@Nix回答你一样,你可以使用Underscore库的 pluck

  

提取属性值列表


示例:

this.form.Emails = [
                    {text: 'moe', id: 1},
                    {text: 'larry', id: 2},
                    {text: 'curly', id: 3}
                   ];

var emails = _.pluck(this.form.Emails, 'text');

//ouput
 ["moe", "larry", "curly"]

无论如何,我建议你保留你的代码版本(又名angular.forEach)。因为维护代码的其他程序员必须知道pluck的作用。

简化您的代码