I’ve simplified the below array to prevent confusion. Here’s my progress up to now… I’ve attempted various methods to generate a fresh array, but haven’t succeeded yet. I’ve even scoured Stack Overflow for solutions. The result is accurate, but unfortunately, I’m only receiving a single object in the fieldsArray. I believe I’m close to the solution with my current approach. Any assistance you can offer would be greatly appreciated. Thank you.
I have the following array.
[
{
fileName: 'Home_AIT_Spraying-1_29062023.agdata',
fileType: 'AgData',
metadata: {
tags: [
{
type: 'Field',
value: 'Home',
uniqueId: null,
},
{
type: 'Farm',
value: 'OTF',
uniqueId: null,
},
{
type: 'Grower',
value: 'Jim Smith',
uniqueId: null,
}
],
},
},
{
fileName: 'AIT_AIT_Spraying-1_30062023.agdata',
fileType: 'AgData',
metadata: {
tags: [
{
type: 'Field',
value: 'Oscar',
uniqueId: null,
},
{
type: 'Farm',
value: 'OTF',
uniqueId: null,
},
{
type: 'Grower',
value: 'Jasper Jones',
uniqueId: null,
}
],
},
}
]
I’d like the output to look like the following:
[
{
Field: 'AIT',
Farm: 'OTF',
Grower: 'Jim Smith',
},
{
Field: 'Oscar',
Farm: 'OTF',
Grower: 'Jasper Jones',
},
];
And my current solution:
const fieldsMetaArray = [] as [];
data.forEach((val, key) => {
console.log('key', key);
val.metadata.tags.map((tag) => {
console.log(`key: ${tag.type} value: ${tag.value}`);
fieldsMetaArray[tag.type] = tag.value;
});
});
console.log(fieldsArray);