下划线js从数组中检索数据

时间:2016-06-20 08:53:09

标签: angularjs underscore.js

我已分配任务以使用下划线js获取数据。

这是我的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"
                }
             ]
          }
      ]
   }
 }

从此我只想"名字"领域。通过这样做:

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

我得到了所有" boardingPointDetails"。结果如下:

              [ [
                {
                    "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"
                }
            ],[
                 {
                 ... 
                 }
              ]
          ...
           ]

帮助我只检索" name"从此。

4 个答案:

答案 0 :(得分:2)

如果您只想要一些名称,如[' Koyambedu',' Madurai'],那么下面的代码就可以了。

$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;
                });

请参阅以下链接:

http://underscorejs.org/#flatten

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

答案 1 :(得分:0)

如果您只想要名字数组,请点击这里:

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

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

答案 2 :(得分:0)

var temp = _.pluck(_.flatten($scope.bdata),'name')

temp将有["Koyambedu", "Nerkundram", "Adyar", "Madurai"]

答案 3 :(得分:0)

试试这个。

$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>