DEV Community

Vepa Mirzayev
Vepa Mirzayev

Posted on

In javascript make mapping json objects with specified keys

You have an array of objects what you want to do is map through them and return specific key value pairs in a new array.
The Functions Code SS

The Functions Code SS

1. For example you have specific keys array

For example you have specific keys array

2. The JSON Object you want to manipulate

Json Objects

3. Result the mapped JSON Objects

The Result

const visibleColumnFields =  ['LicencePlate', 'car', 'CarAttributes', 'ReservationStatus', 'ReservationType', 'CustomerGroupName'];

// The original json objects
      let rows =  [{
        "LicencePlate": "ZPL 524",
        "car": "Opel Corsa",
        "CarAttributes": "Z",
        "ReservationStatus": "İptal Edildi",
        "ReservationType": "Yıllık İç Müşteri",
        "CustomerGroupName": "Dev Test Technology",
        "CompanyName": "İş Geliştirme Müdürlüğü",
        "Zimmet": "Petor Petor ",
        "LocationName": "Test Company Location",
        "EndDateTime": "28.04.2022 00:00",
        "RentDays": "1 Gün",
        "TotalPrice": "350.00",
        "CurrencyType": "Lira",
        "CurrencySymbol": "₺",
        "TotalPriceByCurrency": "350.00",
        "CurrencyRate": "1"
      }];

// Mapping over the objects
      let retwriteRow =   rows.map( function (e) {
        return Object.keys(e).filter((key) => visibleColumnFields.includes(key)).reduce( (cur, key) => {
          return Object.assign(cur, {[key] : e[key]})
        },{})
      });
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
azamat1696 profile image
AZAMAT YADYGEROV

Thank you