3

i have the following array:

items: [
{
    id: "0",
    name: "כיבוי אורות",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "1",
    name: "הדלקת אורות",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "0",
    name: "תנור מטבח",
    icon: "rooms_salon.svg",
    type: "heater",
    status: 0
},
{
    id: "1",
    name: "מזגן מטבח",
    icon: "rooms_salon.svg",
    type: "ac",
    status: 0
}]

i need to filter it so i get only unique item.type and the count of the original amount of items per type.

the resolute i need:

items: [
    {
        type: "scenario",
        amount: 2
    },
    {
        type: "heater",
        amount: 1
    },
    {
        type: "ac",
        amount: 1
    }
]

What would be the best way to do so?

P.O: i need to filter it in the controller, not in ng-repeat.

Thanks allot

Avi

2 Answers 2

12

You could use the angular-filtermodule to group your items:

$scope.items = [
{
    id: "0",
    name: "כיבוי אורות",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "1",
    name: "הדלקת אורות",
    icon: "rooms_salon.svg",
    type: "scenario",
    status: 1
},
{
    id: "0",
    name: "תנור מטבח",
    icon: "rooms_salon.svg",
    type: "heater",
    status: 0
},
{
    id: "1",
    name: "מזגן מטבח",
    icon: "rooms_salon.svg",
    type: "ac",
    status: 0
}]

$scope.content = $filter('countBy')($scope.items,'type');

Here you have a working plunker

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

1 Comment

Thank you. The angular-filter module is a lot cleaner, and easier than writing my own Javascript to get this functionality and create new Objects each time. +1
2

You are not doing a filtering , you need to create a new array with desired values.

or

created a computed filtered value like this

function Ctrl($scope){
    $scope.items = [
        {
            id: "0",
            name: "כיבוי אורות",
            icon: "rooms_salon.svg",
            type: "scenario",
            status: 1
        },
        {
            id: "1",
            name: "הדלקת אורות",
            icon: "rooms_salon.svg",
            type: "scenario",
            status: 1
        },
        {
            id: "0",
            name: "תנור מטבח",
            icon: "rooms_salon.svg",
            type: "heater",
            status: 0
        },
        {
            id: "1",
            name: "מזגן מטבח",
            icon: "rooms_salon.svg",
            type: "ac",
            status: 0
        }];

    Object.defineProperty($scope, 'filtered', {
        get: function(){
            var list = {};
            $scope.items.forEach(function (item) {
                if (list[item.type] === undefined) {
                     list[item.type] = 1;
                } else {
                   list[item.type] += 1;
                }
            });          
            var newItems = [];    
            Object.keys(list).forEach(function(key){      
              newItems.push({  
                 type :key,
                 amount: list[key]
              });    
            });
            return newItems;
        }
    });
}

FIDDLE

5 Comments

Thanks for your reply. i need it to be a filter so when the items array in the scope changes, this will change as well. Thanks again Avi
if you want to make the change along with that add this as a method to your controller.
i need that every item that is being added to the items array, will be also added to the filtered array
OK.. i edited that with a scope value and returned the desired value as filter
That's perfect. Thanks allot

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.