采多属性 [英] Plucking Multiple Properties

查看:93
本文介绍了采多属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从AngularJS中的对象创建一个数组,以用于ui-bootstrap的预输入.

I am trying to create an array from an object in AngularJS to use for ui-bootstrap's typeahead.

我能够从对象(蝙蝠侠")中提取单个值,但我需要能够组合多个属性来创建新的数组元素(蝙蝠侠(Wayne,布鲁斯)").我尝试了$scope.heroList = _.pluck($scope.heroes, 'name' + '(' + 'lname' + ', ' + 'fname' + ')' );,但得到了所有空值.

I am able to pluck a single value out of the object ('Batman') but I need to be able to combine several properties to create a new array element ('Batman (Wayne, Bruce)). I tried $scope.heroList = _.pluck($scope.heroes, 'name' + '(' + 'lname' + ', ' + 'fname' + ')' ); but got all nulls.

这是一个朋克车.

我需要一个看起来像这样的最终数组:

I need a final array that looks like this:

$scope.heroList = ['Batman (Wayne, Bruce]', 'Daredevil (Murdoch, Jack'), ... ];

这是我的JavaScript:

Here is my javascript:

var app = angular.module('app',[]);

app.controller('MainController', function($scope){

  $scope.heroes = [
    {
      id: 1,
      name: 'Iron Man',
      fname: 'Tony',
      lname: 'Stark',
      location: 'Stark Tower',
      comic: 'Marvel'
    },
    {
      id: 2,
      name: 'Batman',
      fname: 'Bruce',
      lname: 'Wayne',
      location: 'Bat Cave',
      comic: 'DC'
    },
    {
      id: 3,
      name: 'Superman',
      fname: 'Clark',
      lname: 'Kent',
      location: 'Metroplis',
      comic: 'DC'
    },
    {
      id: 1,
      name: 'Daredevil',
      fname: 'Jack',
      lname: 'Murdock',
      location: 'Court Room',
      comic: 'Marvel'
    },
    {
      id: 5,
      name: 'Flash',
      fname: 'Barry',
      lname: 'Allen',
      location: 'Speedline',
      comic: 'DC'
    },
    {
      id: 6,
      name: 'Hulk',
      fname: 'Bruce',
      lname: 'Banner',
      location: 'Labratory',
      comic: 'Marvel'
    },
    {
      id: 7,
      name: 'Hawkeye',
      fname: 'Clint',
      lname: 'Barton',
      location: 'Nest',
      comic: 'Marvel'
    },
    {
      id: 8,
      name: 'Thor',
      fname: 'Donald',
      lname: 'Blake',
      location: 'Asgard',
      comic: 'Marvel'
    }
  ];

  $scope.heroList = _.pluck($scope.heroes, 'name');
  //$scope.heroList = _.pluck($scope.heroes, 'name' + '(' + 'lname' + ', ' + 'fname' + ')' );

}); // end MainController

推荐答案

您可以使用回调方法.

You can use callback method.

使用

$scope.heroList = _.pluck($scope.heroes, function(elm){
    return elm.name + '(' + elm.lname + ', ' + elm.fname + ')';
});

演示

我也建议您使用 _.map()

Also I would recommend you to use _.map()

$scope.heroList = _.map($scope.heroes, function(elm){
    return elm.name + '(' + elm.lname + ', ' + elm.fname + ')';
});

带有地图的演示

这篇关于采多属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆