获取数组Angular的单个属性

时间:2015-01-21 14:23:45

标签: javascript arrays angularjs variables

我尝试了很多方法,但我不知道如何获得数组的单个属性。

我想做到这一点 ARRAY1:

 this.array1 = [{name: 'misko', gender: 'male'},{name: 'misko', gender: 'male'}];

然后我想只从阵列1到数组获取名称但是如何

     this.array2 = [{'misko'},{'misko'}];

我的代码

        this.getList = function() {

        this.timesheets = Timesheets.query({
            projectId: $scope.selected.project[0],
            startWeek: this.weekStart,
            endWeek: this.weekEnd
        });
        $log.log('1');
        this.timesheetsId = this.timesheets.map(function (el) {
            return { name: el._id };
            $log.log('2');
        });

    };

我可以在控制台中看到日志1而不是log2

1 个答案:

答案 0 :(得分:3)

您可以使用map,就像这样

this.array1 = [{name: 'misko', gender: 'male'},{name: 'misko', gender: 'male'}];

this.array2 = this.array1.map(function (el) {
   return { name: el.name };
})