omenclature
omenclature

Reputation: 95

ng-repeat filter by existing array values

so I'm working with a basic product category model to get my head around filtering and I can't figure out how to extract a property value from one object within an array while repeating through another.

A simplified version of my category array, which is in scope, looks like this. I can output their names with the preceding directive and the results are as expected:

[{
    "_id": "TY76",
    "name": "Planes"
}, {
    "_id": "887T",
    "name": "Trains"
}, {
    "_id": "A0K4",
    "name": "Autos"
}]

<p ng-repeat="category in product.categories "> {{ category.name }}</p>

And here is a simplified product, also in scope, which may contain the ID of one or more categories. In this case, Bobble Head belongs to both Planes and Autos:

{
    "_id": "9876",
    "name": "Bobble Head",
    "cats": "['TY76','A0K4']"
}

Now, here is where I'm having a hard time. I need to output the category names with the product. I can output the IDs no problem via:

<p ng-repeat="cat in product.cats ">{{ cat }}</p>

But that's of no use to the end user but I have no idea how to end up with something like:

Product: Bobble Head | Categories: Planes, Autos

I don't have the autonomy to add the category name to each product and I've tried a bunch of different filtering approaches but I don't think I'm wording my question right or something because I'm not finding much on the interwebs about this.

Any ideas?

Upvotes: 1

Views: 177

Answers (3)

lenilsondc
lenilsondc

Reputation: 9810

I sugest using a custom filter on categories array.

myApp.filter('byCategoryIds', function() {
    return function(categories, catIds) {
        return categories.filter(function(item) {
            return (catIds.indexOf(item._id) != -1);
        });
    };
});

Then you can iterate on categori array sending ids array like so:

<b>Product:</b> 
{{product.name}}; 
<b>Categories:</b> 
<span ng-repeat="cat in categories | byCategoryIds: product.cats">{{ cat.name }}, </span>

Upvotes: 0

yvesmancera
yvesmancera

Reputation: 2925

You can create a key-pair object where the key is the id and the value is the name of the category:

var categoriesArray = [{
    "_id": "TY76",
    "name": "Planes"
}, {
    "_id": "887T",
    "name": "Trains"
}, {
    "_id": "A0K4",
    "name": "Autos"
}];

$scope.categoriesMap = {};
categoriesArray.forEach(function(category) {
    $scope.categoriesMap[category._id] = category.name;
});

Then in your view you can access the category name like this:

<div ng-repeat="product in products">
    <strong>Product: </strong> {{product.name}} |
    <strong>Categories: </strong> <span ng-repeat="category in product.cats">
      {{categoriesMap[category]}}
    </span>
</div>

Here's a plunkr: http://plnkr.co/edit/BpBcCizzU2Vh8VPniiHA?p=preview

Upvotes: 0

Jack
Jack

Reputation: 21193

Sounds like you want to build up a lookup for category id to category name:

 var categories = [{
      "_id": "TY76",
      "name": "Planes"
  }, {
      "_id": "887T",
      "name": "Trains"
  }, {
      "_id": "A0K4",
      "name": "Autos"
  }];

  // build a category lookup id -> name
  var categoryLookup = {};
  categories.forEach(function(category) {
    categoryLookup[category._id] = category.name;
  });

Here's a full working fiddle: http://jsfiddle.net/02qadem7/1/

Upvotes: 2

Related Questions