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](https://res.cloudinary.com/practicaldev/image/fetch/s--doC8S-BA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8hp79sz73lfa2o2w4ue.png)
1. For example you have specific keys array
![For example you have specific keys array](https://res.cloudinary.com/practicaldev/image/fetch/s--0U_R2Bpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6vwt1xxlpcyyieh6tc9.png)
2. The JSON Object you want to manipulate
![Json Objects](https://res.cloudinary.com/practicaldev/image/fetch/s--c2Jy_W3V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u14nvdfthw7jxndqlcrt.png)
3. Result the mapped JSON Objects
![The Result](https://res.cloudinary.com/practicaldev/image/fetch/s--Zqog3g7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr3jp00sovp5iwtoawvl.png)
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]})
},{})
});
Top comments (1)
Thank you