Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 794
Mohammed Gadi Avatar asked Dec 02 '22 13:12

Mohammed Gadi


1 Answers

Using lodash:

_.map(yourArray, (el => _.pick(el, ['PlaceKey', 'PlaceName', 'Lat', 'Long'])))
like image 58
Egor Stambakio Avatar answered Dec 06 '22 11:12

Egor Stambakio