Create new array using values from another array

74 views Asked by At

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);

3

There are 3 answers

3
Jaromanda X On BEST ANSWER

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);

1
Joshua George On

You need to create an object for each item in the fieldsMetaArray, not an array and then push each object into the fieldsMetaArray instead of directly assigning values to its properties.

Since you want the output to include only 'Field', 'Farm', and 'Grower', you should filter out other types during mapping.

const fieldsMetaArray = [];

data.forEach((val) => {
    const fieldMeta = {}; // Create a new object for each item
    val.metadata.tags.forEach((tag) => {
        // Check if the tag type is 'Field', 'Farm', or 'Grower'
        if (['Field', 'Farm', 'Grower'].includes(tag.type)) {
            // Assign the value to the corresponding property in the object
            fieldMeta[tag.type] = tag.value;
        }
    });
    // Push the object into the fieldsMetaArray
    fieldsMetaArray.push(fieldMeta);
});

console.log(fieldsMetaArray);
0
mat.twg On

let arr = [
  {
    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,
        },
        {
          type: 'Product',
          value: 'Water',
          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,
        },
        {
          type: 'Product',
          value: 'Water',
          uniqueId: null,
        },
      ],
    },
  },
];

console.log(
  arr
    .map((entity) => entity.metadata.tags)
    .map((tags) =>
      Object.assign({}, ...tags.map((tag) => ({ [tag.type]: tag.value }))),
    ),
);