Unwanted undefined key in object

Starting from an array like this:
gridColumns =
[
{ field: "exam_Internalcode", title: 'Exam ID' },
{ field: "patient", title: 'Patient Name' },
]
gridColumns =
[
{ field: "exam_Internalcode", title: 'Exam ID' },
{ field: "patient", title: 'Patient Name' },
]
And an array like this
gridData = [
{ exam_Internalcode: 'I-12M4', patient: 'Mark Lenner', idblock: 125 },
...
]
gridData = [
{ exam_Internalcode: 'I-12M4', patient: 'Mark Lenner', idblock: 125 },
...
]
What's the best way to obtain an array like this?
result = [
{'Exam ID': 'I-12M4', 'Patient Name': 'Mark Lenner'} //no idblock because there is no field for it in gridColumns
...
]
result = [
{'Exam ID': 'I-12M4', 'Patient Name': 'Mark Lenner'} //no idblock because there is no field for it in gridColumns
...
]
Current solution https://jsfiddle.net/n00bCod3r/jdc1hu5p/ However, I get an undefined key and I don't understand why
6 Replies
Jochem
Jochem•3y ago
there's no field definition for block_state that is triggering your null deference operator, which is returning undefined instead of throwing an error. You could do a similar check like you do on the value and use a ternary operator to set a default value other than undefined
MC23
MC23OP•3y ago
hmm i'll try
res.forEach(el => delete el["undefined"]);

console.log(res)
res.forEach(el => delete el["undefined"]);

console.log(res)
The only solution that came to me, what did you have in mind?
Jochem
Jochem•3y ago
.map(([key, value]) => {
const newKey = exportedColumns.find(({ field }) => field === key)?.title;
return [ newKey ? newKey:key,
value === null ? '' : value]
})
.map(([key, value]) => {
const newKey = exportedColumns.find(({ field }) => field === key)?.title;
return [ newKey ? newKey:key,
value === null ? '' : value]
})
this way, each key is represented, even if it's not in the mapping array, just with the original value instead of the translated one
MC23
MC23OP•3y ago
I don't want the original key tho, if it's not present
[
{
//NOT THESE
"idblock": "Lbl",
"idMaterial": "1kVy",
"idexam": "yxDN",
"barcode": "B00000167580000001564",
"block_state": "INSERITO",
//ONLY THESE:
"ID Esame": "22-I-00114",
"Paziente": "MARCO BERTONE",
"Data Accett.": "27/07/2022 09:22",
"Data Ins.": "27/07/2022 09:24",
"Data Consegna Ref.": "",
"Data Cambio Stato": "",
"Cod. Blocchetto": "B-2",
"Desc. Blocchetto": "BLock1",
"Desc. Materiale": "AORTA",
"Stato Materiale": "IN TOTO",
"Utente Cambio Stato": ""
}
]
[
{
//NOT THESE
"idblock": "Lbl",
"idMaterial": "1kVy",
"idexam": "yxDN",
"barcode": "B00000167580000001564",
"block_state": "INSERITO",
//ONLY THESE:
"ID Esame": "22-I-00114",
"Paziente": "MARCO BERTONE",
"Data Accett.": "27/07/2022 09:22",
"Data Ins.": "27/07/2022 09:24",
"Data Consegna Ref.": "",
"Data Cambio Stato": "",
"Cod. Blocchetto": "B-2",
"Desc. Blocchetto": "BLock1",
"Desc. Materiale": "AORTA",
"Stato Materiale": "IN TOTO",
"Utente Cambio Stato": ""
}
]
The other keys are added That's why I added the comment in my first message, I apologise if this wasn't clear My solution work even if it's not the best
Jochem
Jochem•3y ago
.reduce((result, [key, value]) => {
const newKey = exportedColumns.find(({ field }) => field === key)?.title;
if (newKey) result.push([newKey, value?value:'']);
return result;
}, [])
.reduce((result, [key, value]) => {
const newKey = exportedColumns.find(({ field }) => field === key)?.title;
if (newKey) result.push([newKey, value?value:'']);
return result;
}, [])
then use this instead of the map function you can add some newKey === undefined and reuse your own value ternary if you want, I was just being lazy 🙂
MC23
MC23OP•3y ago
ok, ty
Want results from more Discord servers?
Add your server