manipulating json

Hello,
I’m a brand new user of Observable (it sounds so fantastic! Thanks for such a great platform!).
I have difficulty handling json according to my needs. I got lost with .nest .map etc.

I have a simple csv file. I load it into my notebook and the result below is displayed:

data = Array(x items) [
0: Object {header1: "value1", header2: "value2", header3: "value3", etc.}
1: Object {header1: "value1", header2: "value2", header3: "value3", etc.}
2: Object {header1: "value1", header2: "value2", header3: "value3", etc.}
...more
]

data  = FileAttachment("mycsv.csv").csv()

I need to get a result like this:

jsonToDownload = Object {
value1: Object <!-- from data.object[0] => header1 --> {
        header2: "value2"
        header3: "value3"
        etc.
        }
value1: Object <!-- from data.object[1] => header1 --> {
        header2: "value2"
        header3: "value3"
        etc.
        }
value1: Object <!-- from data.object[2] => header1 --> {
        header2: "value2"
        header3: "value3"
        etc.
        }
...more
}

What’s the best way to do this?
Thanks in advance for helping out!

try Object.fromEntries?

Object.fromEntries(data.map(d => [d.header1, d]))

the only difference with your requirement is that this doesn’t remove header1 from the object. To do this, we need a bit more wrangling:

Object.fromEntries(
  data.map(d => [
    d.header1,
    Object.fromEntries(Object.entries(d).filter(([k]) => k !== "header1"))
  ])
)
1 Like

:pray: it’s perfect! thank you very much