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

Store certain values in JSON into parameters in React

brand new to Javascript.

I have figured out how to get data back from a Monday.com board, and it is returned as a JSON value like this:

{
"boards": [
    {
        "items": [
            {
                "column_values": [
                    {
                        "id": "location",
                        "title": "Location",
                        "type": "location",
                        "text": "Cape Town, South Africa"
                    }
                ]
            },
            {
                "column_values": [
                    {
                        "id": "location",
                        "title": "Location",
                        "type": "location",
                        "text": "Paris, France"
                    }
                ]
            },
            {
                "column_values": [
                    {
                        "id": "location",
                        "title": "Location",
                        "type": "location",
                        "text": "New York, NY, USA"
                    }
                ]
            }
        ]
    }
]

}

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

There are 3 items here, but this amount can be anything.

I need to get the location text (e.g. "Cape Town, South Africa") and then I need to use this later in an API call to get the weather of this location.

I finally managed to display in the console the location, like this:

console.log(JSON.stringify(this.state.boardData.boards[0].items[0].column_values[0].text));

Obviously this then displays what I want, but only the value of the first item.

I need to do this dynamically to get all the locations, and I need to store this into variables, so that I can then make an API call to get the weather of each of the 3 (or more) locations.

I am totally lost and have been struggling for hours. Once I manage to do this, I still have no idea how I’ll make the API call but one thing at a time 🙂

>Solution :

You can extract the text with some amount of nested .map. Map boards => map items => map column_values. You will get an array of arrays of arrays. And then you can apply .flat() to unwrap them all. (.flat(Infinity); also can be used)

const apiData={boards:[{items:[{column_values:[{id:"location",title:"Location",type:"location",text:"Cape Town, South Africa"}]},{column_values:[{id:"location",title:"Location",type:"location",text:"Paris, France"}]},{column_values:[{id:"location",title:"Location",type:"location",text:"New York, NY, USA"}]}]}]};

const locations = apiData.boards
  .map((board) => board.items.map((i) => i.column_values.map((cv) => cv.text)))
  .flat(2);
console.log(locations);
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