2016-03-05 152 views
0

我需要從數組中獲取具有特殊屬性「類型」的對象。這些對象我將分配給範圍。我怎樣才能做到這一點?如何從數組中獲取特定對象的屬性?

下面的方法對我來說沒有解決。

$scope.vendors = {} 
$scope.clients = {} 

$scope.loadCounterparties = function() { 
    Counterpartie.query(function(response) { 

    $scope.vendors = response.type.Vendor; 
    $scope.clients = response.type.Client 

    }); 
}; 

響應物體看起來像這樣

enter image description here

提前感謝!

回答

1

Angular沒有專門的東西。您需要通過純java腳本過濾數組。然而,你可以嘗試使用3rd party library by the name underscore.js. 它增加了許多有用的功能,如「在哪裏」:

_.where(列表屬性) 會仔細檢查該列表中的每個值,返回所有包含所有的值的數組屬性中列出的鍵值對。

_.where(listOfPlays,{author:「Shakespeare」,year:1611}); => [{標題: 「辛白林」,作者: 「莎士比亞」,年:1611}, {標題: 「暴風雨」,作者: 「莎士比亞」,年:1611}]

這裏是圖書館的網頁 http://underscorejs.org/#where

+0

謝謝你,丹尼斯! –

1

鏈接可以使用角度的forEach,但我會用lodash

// assuming one array and two search arguments i.e. client and vendor 
var data = response; 
$scope.loadCounterparties = _.filter(data, {type: 'Vendor', type:  'Client'}); 
相關問題