Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm having a dropdown list with multiselect option. I want to filter out data from a complex JSON based on that array.

Selected options forms a array of data like:

$scope.myval=["Adyar","Paris","central"];

My JSON :

$scope.myTest={     
    "buslist":
        {
        "code":"1",
        "message":"Success",
        "fromStationCode":"71",
        "searchResult":[        {
        "arrivalTime":"17:00:00",
        "availableSeats":"42",
        "boardingPointDetails":[{
        "code":"1631",
        "name":"Koyambedu",
        "time":"09:30:00"
        },
        {
        "code":"961296",
        "name":"Paris",
        "time":"09:45:00"
        }
        ]
        ]
        },

         {
        "arrivalTime":"18:00:00",
        "availableSeats":"32",
        "boardingPointDetails":[{
        "code":"2084",
        "name":"Adyar",
        "time":"09:30:00"
        },
        {
        "code":"961296",
        "name":"Madurai",
        "time":"09:45:00"
        }
        ]
        ]
        }
        }
        ...
    };

My HTML templating is:

                <tbody ng-repeat=" i in myTest.buslist.searchResult" >
                <tr>
                    <td>{{i.arrivalTime}}</td>
                    <td>{{i.availableSeats}}</td>

                    <td>
                    <p ng-repeat="m in i.boardingPointDetails">{{m.name}}</p>
                    </td>
                </tr>
            </tbody>

I want to filter my data based on selected values. I had tried something like this :

$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.name.indexOf(data);

i.e:selected options must match "name" field in "boardingPointDetails" but it fails. Thanks in advance.

share|improve this question

Since $scope.myTest.buslist.searchResult.boardingPointDetails is an array $scope.myTest.buslist.searchResult.boardingPointDetails.name is not valid.

You need to use an Array function to get the correct result:

$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.filter(function(el) {
   return el.name === data;
}).length > 0;

EDIT:

Due to your comments I understand you want to get the boardPointDetails that has the same name property as one of the data options. Where data is an Array of strings.

This will do the job:

$scope.matched = $scope.myTest.buslist.searchResult.boardingPointDetails.filter(function(el) {
   return data.indexOf(el.name) === 1;
});
share|improve this answer
    
It does n't works..:( – keerthi Jun 21 at 11:49
    
what does data contain? what is it's structure? – Asaf David Jun 21 at 11:55
    
data is nothing but selected options. It is in array format.$scope.myval=["Adyar","Paris","central"]; – keerthi Jun 21 at 12:08
    
so data is an Array of strings. What is the condition? Do all those values need to be in boardingPointDetails? Or just one of them? – Asaf David Jun 21 at 12:26
    
boardingPointDetails that matches any one of the value in array should be displayed as output. – keerthi Jun 21 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.