0

I need to get objects with special attribute "type" out of Array. These objects I am going to assign to scope. How can I do this?

The following approach didn't work out for me.

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

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

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

  });
};

Response objects look like this

enter image description here

Thanks in advance!

2 Answers 2

1

Angular doesn't have something dedicated for this. You need to filter the arrays via plain java script. However you can try using a 3rd party library by the name underscore.js. It adds many usefull functions like "where":

_.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.

_.where(listOfPlays, {author: "Shakespeare", year: 1611}); => [{title: "Cymbeline", author: "Shakespeare", year: 1611}, {title: "The Tempest", author: "Shakespeare", year: 1611}]

Here is a link to the library's page http://underscorejs.org/#where

Sign up to request clarification or add additional context in comments.

Comments

1

can use angular forEach but I would use lodash

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.