Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a new array from current Airtable array

How can I get this old array:

[
    {
        "_table": {
            "_base": {
                "_airtable": {},
                "_id": "appDxGpKPJl3Exjs2"
            },
            "id": null,
            "name": "PropMatters"
        },
        "id": "recGBOdnDtrvaEu7N",
        "_rawJson": {
            "id": "recGBOdnDtrvaEu7N",
            "createdTime": "2022-05-24T07:05:22.000Z",
            "fields": {
                "Name": "Mon 23 May, Tues 24 May",
                "KeyID": "2024f0d10106261cdc852194042d731c"
            }
        },
        "fields": {
            "Name": "Mon 23 May, Tues 24 May",
            "KeyID": "2024f0d10106261cdc852194042d731c"
        }
    },
    {
        "_table": {
            "_base": {
                "_airtable": {},
                "_id": "appDxGpKPJl3Exjs2"
            },
            "id": null,
            "name": "PropMatters"
        },
        "id": "recS98niJqeUmlQ1n",
        "_rawJson": {
            "id": "recS98niJqeUmlQ1n",
            "createdTime": "2022-05-24T07:59:22.000Z",
            "fields": {
                "Name": "Mediator Demo",
                "KeyID": "eb0e79a50044cf02c1969e7f20093788"
            }
        },
        "fields": {
            "Name": "Mediator Demo",
            "KeyID": "eb0e79a50044cf02c1969e7f20093788"
        }
    }
]

To output something like this:

[ 
   {
     "Name": "Mon 23 May, Tues 24 May",
     "KeyID": "2024f0d10106261cdc852194042d731c"
   },
   {
     "Name": "Mediator Demo",
     "KeyID": "eb0e79a50044cf02c1969e7f20093788"
   },
]

So as you can see, I’d like to output a new array simplifying the old JSON array to loop through each "fields" array item.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have a feeling I’d use js maparray function, but I get lost if it’s more than a simple array.

>Solution :

The objects that you want in your array just correspond to the "fields" key in the "old" array. So you can just map the array:

const oldArray = JSON.parse(json);
const newArray = oldArray.map(entry => entry.fields);

And turn it back to JSON if needed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading