Before we proceed, let's reference this question: Angularjs map array to another array
Presently, I am managing two distinct scopes:
$scope.products = [
{id: 001, name: "prod 1", ...},
{id: 002, name: "prod 2", ...},
{id: 003, name: "prod 3", ...},
{id: 004, name: "prod 4", ...},
{id: 005, name: "prod 5", ...}
]
$scope.cart = {
products: [001,002]
...
}
The collection $scope.products
contains details about all available products, while $scope.cart.products
holds the IDs of items in the cart.
The linked answer demonstrates merging these arrays. Nonetheless, I am keen on maintaining their separation and establishing a mapping between them. Is that feasible? Should I opt for a custom filter within the repeater, or does Angular offer a native solution? Appreciate any guidance
UPDATE
Utilizing a Filter:
filter('mapProducts', function($filter) {
return function(products, ids) {
var result;
result = [];
$filter('filter')(products, function(p) {
if (ids.indexOf(p.id) !== -1) {
return result.push(p);
}
});
return result;
};
});
Incorporate this in the repeater:
<div ng-repeat="product in products | mapProducts:cart.products">