M
M
microf2015-09-29 22:04:46
JavaScript
microf, 2015-09-29 22:04:46

Split data into an array or not?

I have a json with messages that are characterized by "read" and "not read" - status1 or status2. It is necessary to display them in the view in the appropriate columns. You can make a filter on the page, but it seems to me that you can pull out the data completely by the service and split it into two arrays in the controller, and then output it with two ng-repeat. Question 1 - is it correct? and second - how best to split an object into two, depending on the status?

[{
        "id": 1,
        "title":"message from admin",
        "name_message": "Message sent",
        "category": "qa",
        "status" : "1",
        "time": "15 min ago"
    },
    {
        "id": 2,
        "title":"message from QA",
        "name_message": "Need to paint",
        "category": "recommendation",
        "status" : "2",
        "time": "15 min ago"
    },

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Shashkov, 2015-09-29
@microf

1. Better 2 ng-repeat and 2 arrays, the fewer expressions in the template - the fewer watchers, the greater the performance.
2. You can use Underscore, for example. Something like this:

$scope.array1 = _.filter(items, function(item){
    return item.status == 1;
});

$scope.array2 = _.filter(items, function(item){
    return item.status == 2;
});

Z
Zakharov Alexander, 2015-09-29
@AlexZaharow

In my personal opinion, I would not change the configuration of the data that I received from the server. Two ng-repeat is better. Who knows why you will need this array in the future, and you have already gutted it. )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question