如何使用 lodash.js 或 underscore.js 过滤 json?

How can I filter json using lodash.js or underscore.js?

我正在努力过滤下面写的 json 以获得想要的结果。

[{
        "Key": "EShSOKthupE=",
        "ImageUrl": "path",
        "Title": "ABC", 
        "CityId": 16, 
        "TimezoneShortName": "PYT",
        "UtcHrsDiff": 8.5,
        "PlaceKey": "QIZHdWOa77o=",
        "PlaceName": "Shymala Hills Slums",
        "Lat": 23.2424856,
        "Long": 77.39488289999997,
        "ActivityList": [ "Test Activity" ]
      },
      {
        "Key": "NXLQpZAZT4M=",
        "ImageUrl": "",
        "Title": "ASAS",  
        "CityId": 17, 
        "TimezoneShortName": "AEST",
        "UtcHrsDiff": 10,
        "PlaceKey": "o4fAkahBzYY=",
        "PlaceName": "ASAS",
        "Lat": 12.9856503,
        "Long": 77.60569269999996,
        "ActivityList": [ "Adventure Sport" ]
      }]

现在我想使用 lodash 或 undescore js 从上面 json 得到这样的 json。

[{
    "PlaceKey": "QIZHdWOa77o=",
    "PlaceName": "ABC",
    "Lat": 23.2424856,
    "Long": 77.39488289999997
  },
  {
    "PlaceKey": "o4fAkahBzYY=",
    "PlaceName": "ASAS",
    "Lat": 12.9856503,
    "Long": 77.60569269999996,
  }]

我可以得到任何帮助吗?

您可以在 Javascript 中轻松完成此操作,无需任何外部库,例如:

filteredArray=arr.map(function(item){
  return {
    "PlaceKey": item.PlaceKey,
    "PlaceName": item.PlaceName,
    "Lat": item.Lat,
    "Long": item.Long,
  }
})

使用 lodash:

_.map(yourArray, (el => _.pick(el, ['PlaceKey', 'PlaceName', 'Lat', 'Long'])))

使用_.map函数

var finalArray = _.map(your_arr, function(obj) {
    return {
    "PlaceKey": obj.PlaceKey,
    "PlaceName": obj.PlaceName,
    "Lat": obj.Lat,
    "Long": obj.Long,
  }
});

或者只使用数组 map 函数:

var finalArray = your_arr.map(function(obj) {
   return {
     "PlaceKey": obj.PlaceKey,
     "PlaceName": obj.PlaceName,
     "Lat": obj.Lat,
     "Long": obj.Long,
   }
});