Search code examples
javascriptarraysmultidimensional-arraykey-value

Create new array using values from another array


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: 'Home',
        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);


Solution

  • Using Array::map x2 and Object.fromEntries makes the solution very simple - in fact, it's a single line of code (split here for readability)

    const input = [ { 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, } ], }, } ];
    
    
    const result = input.map(({ metadata: { tags } }) =>
        Object.fromEntries(tags.map(({ type, value }) => [type, value]))
    );
    
    
    console.log(result);