Is there a way in the array.map() method to access / iterate the keys for every key/value-pair in each index?
Please note that I am aware of the Index parameter, which however only returns the index/key of the entire row - not of the respective keys of pairs in the rows.
In NodeJS Express I receive the following data in req.body from a POST-request:
[
{ ptiid: '2', ptiname: 'PTI26-Profile-1', order: '1', size: 25 },
{ ptiid: '5', ptiname: 'PTI29-Profile-1', order: '2', size: 25 },
{ ptiid: '3', ptiname: 'PTI27-Profile-1', order: '3', size: 25 },
{ ptiid: '4', ptiname: 'PTI28-Profile-1', order: '4', size: 25 }
]
The goal is to find out whether I could use .map() to shorten my route-files in general. Up to this point I have been using FOR OF loops (for multi row requests because of await) and manual declarations of variables, in order to prepare the MYSQL Query:
let ptiId = req.body.ptiid
let ptiName = req.body.ptiname
// and so on...
The clean and concise look of .map() appeals to me here. I would like to prepare my MYSQL-Query-Variables accordingly and wonder if I can access the keys:
let elements = req.body //possibly just chaining .map() to req.body directly later
let sqlVars = elements.map(element => {
console.log(`The value ${element.ptiid} is easy to get if I know the key`)
console.log(`If only I could get a key like ${element.key}, then I could call all values by their keys`)
})
Please just let me know if this is not possible - then I just abandon the idea. If the suggested code is too long, I would probably not use it - but it would still be good to learn.
Here's a very easy-going way to process the data...
const data =
[
{ ptiid: '2', ptiname: 'PTI26-Profile-1', order: '1', size: 25 },
{ ptiid: '5', ptiname: 'PTI29-Profile-1', order: '2', size: 25 },
{ ptiid: '3', ptiname: 'PTI27-Profile-1', order: '3', size: 25 },
{ ptiid: '4', ptiname: 'PTI28-Profile-1', order: '4', size: 25 },
];
// Generates a indexed object of the data above.
const parsed = Object.fromEntries
(
data.map
(
// Extract the known properties...
({ ptiid, ptiname, order, size }, position) =>
{
// Note that you can replace `{ ptiid, ptiname, order, size }`
// directly with `item`, so, the following line is extra stuff.
const item = data[position];
// Extract the unknown properties if needed...
const allKeys = Reflect.ownKeys(item);
// IMPORTANT: You can access all keys like this:
for ( const key of allKeys )
{
const value = item[key];
// Do your things here...
}
// Cast any data to its correct/desired type if needed...
ptiid = Number(ptiid);
order = Number(order);
// Select the index value for the entry result...
const index = ptiname;
const entry = [index, item];
// Just debbuging to know the state of each item...
console.log
({
'Selected Index (from ptiname)': index,
'All Keys': allKeys,
'All Keys/Values': item,
'Known Keys/Values': {ptiid, ptiname, order, size},
});
// Returns a entry-like (pair of key/value) value.
return entry;
}
)
);
console.log('PARSED RESULT:', parsed);
CSS:
.as-console-wrapper { min-height: 100%; }
NOTE: I indexed the final object with the ptiname
property value, but any property value can be used as the index if they're number
, string
or symbol
.