1

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?

1
  • Can you provide us a working fiddle? Commented Dec 30, 2015 at 19:11

3 Answers 3

2

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/

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

1 Comment

Ah, you beat me to it by 3 minutes with an answer very similar to mine, have a vote!
0

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

Comments

0

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>

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.