最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I filter json using lodash.js or underscore.js? - Stack Overflow

programmeradmin3浏览0评论

I am trying banging my head to filter json written below to get desired result.

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

Now I want to get json like this from above json using lodash or undescore js.

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

Any help I can get on this?

I am trying banging my head to filter json written below to get desired result.

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

Now I want to get json like this from above json using lodash or undescore js.

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

Any help I can get on this?

Share Improve this question asked May 19, 2017 at 11:02 Mohammed GadiMohammed Gadi 3915 silver badges18 bronze badges 2
  • I have used filter _.filter to get this but donno exaclty how to get the result I have mentioned. – Mohammed Gadi Commented May 19, 2017 at 11:07
  • I don't understand why this question is marked as negative? – Mohammed Gadi Commented May 19, 2017 at 11:19
Add a ment  | 

3 Answers 3

Reset to default 9

Using lodash:

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

You can do this simply in Javascript, without the need of any external library like :

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

use _.map function

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

or just use the arrays map function:

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

评论列表(0)

  1. 暂无评论