Flattening JSON with Javascript

I have the following code.

function postResponseMap (options) {
  const data=options.postResponseMapData[0].data[0].data
  var record=[]
  var key= new Object()

for(let i=0;i<options.postResponseMapData[0].data[0].data.length;i++){
//while(i> options.postResponseMapData[0].data[0].data.length){
key[options.postResponseMapData[0].data[0].data[i].name] = options.postResponseMapData[0].data[0].data[i].values
return key
//return data[i]
i=i+1
}

}

  • The goal
    • I'm trying to flatten the JSON to be Name:Values
  • Way I'm attempting to go about it
    • Go through each of the objects in postResponseMapData[0].Data[0].data to find those pieces of information to create a key:value pair
  • What doesn't look to be happening.
    • When I run my code, I only get the first value
    • When I tried returning "i" it doesn't iterate or increment
    • Since it's not iterating/incrementing, I'm only getting the first value
  • What I've done so far
    • I tried to use for(x of y) the area that I needed to look for either wasn't iterable or didn't iterate when it meant to
    1. I tried using a while loop which also didn't iterate and only gave me the first value
  • My question
    • What am I doing wrong
    • Is there a better way to accomplish this?
function postResponseMap (options) {
const res = options.postResponseMapData.forEach(l1 => {
l1.data.forEach(l2 => {
l2.data.reduce((acc, cur) => {
acc[cur.name] = cur.values
return acc
}, {})
}
}
}

I didn't test that, but that's how I'd do it. Avoid index access like that, even if you know there will only be a single result, it's cleaner (IMO) to make it dynamic as it will also handle empty arrays where index access will fail.