0

I'm assigned a task to get data using underscore js.

This is my JSON :

$scope.myData= {
"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": "Nerkundram",
                    "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"
                }
             ]
          }
      ]
   }
 }

From this I want only "name" field. By doing this :

$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');

I got all "boardingPointDetails". The result looks like:

              [ [
                {
                    "code": "2084",
                    "name": "Adyar",
                    "time": "09:30:00"
                },
                {
                    "code": "961296",
                    "name": "Madurai",
                    "time": "09:45:00"
                }
             ],[
                {
                    "code": "1631",
                    "name": "Koyambedu",
                    "time": "09:30:00"
                },
                {
                    "code": "961296",
                    "name": "Nerkundram",
                    "time": "09:45:00"
                }
            ],[
                 {
                 ... 
                 }
              ]
          ...
           ]

Help me to retrieve only "name" from this.

3
  • Will array of names do? Like ['Adyar','Nerkundram'] ? Commented Jun 20, 2016 at 9:03
  • yes without using for loop i.e only by means of underscore js and from the result I want to list those data using ng-repeat. Commented Jun 20, 2016 at 9:11
  • I've posted the answer. Commented Jun 20, 2016 at 9:22

4 Answers 4

2

If you just want array of names like ['Koyambedu', 'Madurai'] then below code would work.

$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');

// Flatten the data.
$scope.flatbdata = _.flatten($scope.bdata, true);

$scope.flatbdata = $scope.flatbdata.filter(function(d){
                      return d != undefined && d.hasOwnProperty('name')
                   })

// map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results

$scope.names = $scope.flatbdata.map(function(d){
                    return d.name;
                });

Refer below links :

http://underscorejs.org/#flatten

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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

4 Comments

Thanks for reply..Your code works upto flattern.. Then it fails to map..
Maybe there is some undefined in the flattened data. So, you can filter the data before using map. $scope.flatbdata = $scope.flatbdata.filter(function(d){ return d != undefined && d.hasOwnProperty('name') });
..Thank you so much ...I had been trying this from yesterday..You make my day...It works perfectly..
As you are using resultant array of names in ng-repeat, so either you can use track by in the ng-repeat in this case duplicate will also appear (and no dups error in angular) or you can just take unique from this array using _.uniq function refer this : underscorejs.org/#uniq
0

If you just want the array of names, here it is:

$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');

var res= _.flatten($scope.bdata);
res=_.pluck(res,'name');
console.log(res);

1 Comment

What output are you getting? Because I'm sure it is working. Gave me this op [ 'Koyambedu', 'Nerkundram', 'Adyar', 'Madurai' ]
0
var temp = _.pluck(_.flatten($scope.bdata),'name')

temp will have ["Koyambedu", "Nerkundram", "Adyar", "Madurai"]

1 Comment

what is the output you are looking for? Can you tell the sample?
0

Try this.

$scope = {}; // in real project you don't need this. it's just for snippet.
$scope.myData = {
  "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": "Nerkundram",
        "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"
      }]
    }]
  }
};
$scope.names = _.chain($scope.myData.buslist.searchResult).pluck("boardingPointDetails").flatten(true).map(function(item) {
  return item.name;
}).value();
console.log($scope.names);
<script src="http://underscorejs.ru/underscore-min.js"></script>

2 Comments

But I'm having my array in $scope. Do you want me save in var??
It's no matter, where you save your result.

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.