0

let data=
[
  {
    system: {
      id: '4gSSbjCFEorYXqrgDIP2FA',
      type: 'Entry',
      name: `Author`
    },
    DataDetails: {
      shortSlugOption: {
        "en-us":`some value`
      },
      mediaFileAssetLink: {
        "za-op":`file Linl`
      },
      mediaFileAssetGalary: {
        "hi":`file link 2`
      },
      singleMediaImage: {
        "li-sd-op":`file link single`
      },
    }
  },
  {
    system: {
      id: '1aBOO8tu3lUsjtICuIbUM5',
      type: 'Entry',
      name: `Author`
    },
    DataDetails: {
      short: {
        "en-us":`details of shorts`
      },
      shortSlugOption: {
        "hi-In":`options`
      },
      booleanField: {
        "kl":`true`
      }
    }
  },
  {
    system: {
      id: '2pOUGnI1oRD7nsrYs600HA',
      type: 'Entry',
      name: `testing`
    },
    DataDetails: { testingNewValue: {
      "en-uk":`details value`
    } }
  },
  {
    system: {
      id: '66rzYr2BpWL1VTBHdLTdSW',
      type: 'Entry',
      name: `new`
    },
    DataDetails: { oneReference: {
      "ru-re":`values 1`
    }, multiReference: {
      "uk-es":`values 2`
    } }
  },
  {
    system: {
      id: 'cIb5mqEBRWDD6hrNmFmFE',
      type: 'Entry',
      name: `new`
    },
    DataDetails: { testingNewValue: {
      "hi-IN":`jksdsdo`
    } }
  },
  {
    system: {
      id: '7kRzyt4PFrX13gHcw3Z1Ko',
      type: 'Entry',
      name: `testing`
    },
    DataDetails: { testingNewValue: {
      "en-us":`kknksdo`
    } }
  },
  {
    system: {
      id: '2OspeCtNK0sh2cPiuU9jIz',
      type: 'Entry',
      name: `dummy`
    },
    DataDetails: {
      short: {
        "gh-we":`dfvndkssa`
      },
      shortSlugOption: {
        "jk-cv":`sdocjosmdc`
      },
      mediaFileAssetLink: {
        "en-us":`jdsojocis`
      },
      booleanField: {
        "cb-af":`true`
      }
    }
  },
  {
    system: {
      id: '2eAmIIuG4xkLvatkU3RUSy',
      type: 'Entry',
      name: `dummy`
    },
    DataDetails: { dummy: {
      "en-us":`dshcifdvk`
    }, india: {
      "kh-vh-we":`sdci`
    } }
  },
  {
    system: {
      id: '7hbdS3MgfZ73TOtlu1WfXw',
      type: 'Entry',
      name: `dummy`
    },
    DataDetails: { testingNewValue: {
      "ax-lo":`sdcoklsdc`
    }, locationField: {
      "sd-op":`sdcndkdc`
    } }
  }
];

data.map((value)=>{
  var name = value.system.id;
  var details={};

  details[name]={
    title:value.system.id,
    uid:value.system.id,
    url:`/${value.system.name}/${value.system.id}`,
  }

  console.log(details)
})

I am storing my data in JSON file but by doing some modification in my current object.

data I want to store in JSON is like in this way

{
  "4gSSbjCFEorYXqrgDIP2FA": {
    "title": "4gSSbjCFEorYXqrgDIP2FA",
    "uid": "4gSSbjCFEorYXqrgDIP2FA",
    "url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
    "shortSlugOption": "some value",
    "mediaFileAssetLink": "file Linl"
  },
  "1aBOO8tu3lUsjtICuIbUM5": {
    "title": "1aBOO8tu3lUsjtICuIbUM5",
    "uid": "1aBOO8tu3lUsjtICuIbUM5",
    "url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
    "short": "details of shorts",
    "shortSlugOption": "options",
    "booleanField": "true"
  }
}

so to get entries from the Data object I have created map function to get the value and to fetch the DataDetails object I used Object.entries

for (const [key, value] of Object.entries(value.DataDetails)) {
  console.log(`${key}: ${value}`);
}

so I made some changes in my code to get the desire output the problem is I am getting this

let data=
[
  {
    system: {
      id: '4gSSbjCFEorYXqrgDIP2FA',
      type: 'Entry',
      name: `Author`
    },
    DataDetails: {
      shortSlugOption: {
        "en-us":`some value`
      },
      mediaFileAssetLink: {
        "za-op":`file Linl`
      },
      mediaFileAssetGalary: {
        "hi":`file link 2`
      },
      singleMediaImage: {
        "li-sd-op":`file link single`
      },
    }
  },
  {
    system: {
      id: '1aBOO8tu3lUsjtICuIbUM5',
      type: 'Entry',
      name: `Author`
    },
    DataDetails: {
      short: {
        "en-us":`details of shorts`
      },
      shortSlugOption: {
        "hi-In":`options`
      },
      booleanField: {
        "kl":`true`
      }
    }
  }
];

data.map((value)=>{
  var name = value.system.id;
  var details={};

for (const [key, values] of Object.entries(value.DataDetails)) {

 details[name]={
    title:value.system.id,
    uid:value.system.id,
    url:`/${value.system.name}/${value.system.id}`,
    key:`${values}`
  }
  
}
  console.log(details)
})

I was trying to add ${key} but it's not making me to do so I can't get the DataDetails value as key in my object

is there any possible way I can DataDetails value which are shortSlugOption, mediaFileAssetLink by removing en-us,za-op n all

as my expected value is because the name field are common in this JSON object so I want to store common name field in One JSON file

{
  "4gSSbjCFEorYXqrgDIP2FA": {
    "title": "4gSSbjCFEorYXqrgDIP2FA",
    "uid": "4gSSbjCFEorYXqrgDIP2FA",
    "url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
    "shortSlugOption": "some value",
    "mediaFileAssetLink": "file Linl"
  },
  "1aBOO8tu3lUsjtICuIbUM5": {
    "title": "1aBOO8tu3lUsjtICuIbUM5",
    "uid": "1aBOO8tu3lUsjtICuIbUM5",
    "url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
    "short": "details of shorts",
    "shortSlugOption": "options",
    "booleanField": "true"
  }
}

1 Answer 1

2

Is this what you were looking for. Created the details Object separately and spreaded it in the next step. I took the first and only element coming from the Object.values array

let data=
[
  {
    system: {
      id: '4gSSbjCFEorYXqrgDIP2FA',
      type: 'Entry',
      name: `Author`
    },
    DataDetails: {
      shortSlugOption: {
        "en-us":`some value`
      },
      mediaFileAssetLink: {
        "za-op":`file Linl`
      },
      mediaFileAssetGalary: {
        "hi":`file link 2`
      },
      singleMediaImage: {
        "li-sd-op":`file link single`
      },
    }
  }
];

data.map((value)=>{
  var name = value.system.id;
  var details={};

for (const [key, values] of Object.entries(value.DataDetails)) {

let detailsObj = {}
Object.keys(value.DataDetails).forEach((detail) => {
    detailsObj[detail] = Object.values(value.DataDetails[detail])[0]
})


 details[name]={
    title:value.system.id,
    uid:value.system.id,
    url:`/${value.system.name}/${value.system.id}`,
        ...detailsObj
  }
  
}
  console.log(details)
})

To group by the name can use Array.reduce

let data = [{"system":{"id":"4gSSbjCFEorYXqrgDIP2FA","type":"Entry","name":"Author"},"DataDetails":{"shortSlugOption":{"en-us":"some value"},"mediaFileAssetLink":{"za-op":"file Linl"},"mediaFileAssetGalary":{"hi":"file link 2"},"singleMediaImage":{"li-sd-op":"file link single"}}},{"system":{"id":"1aBOO8tu3lUsjtICuIbUM5","type":"Entry","name":"Author"},"DataDetails":{"short":{"en-us":"details of shorts"},"shortSlugOption":{"hi-In":"options"},"booleanField":{"kl":"true"}}},{"system":{"id":"2pOUGnI1oRD7nsrYs600HA","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-uk":"details value"}}},{"system":{"id":"66rzYr2BpWL1VTBHdLTdSW","type":"Entry","name":"new"},"DataDetails":{"oneReference":{"ru-re":"values 1"},"multiReference":{"uk-es":"values 2"}}},{"system":{"id":"cIb5mqEBRWDD6hrNmFmFE","type":"Entry","name":"new"},"DataDetails":{"testingNewValue":{"hi-IN":"jksdsdo"}}},{"system":{"id":"7kRzyt4PFrX13gHcw3Z1Ko","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-us":"kknksdo"}}},{"system":{"id":"2OspeCtNK0sh2cPiuU9jIz","type":"Entry","name":"dummy"},"DataDetails":{"short":{"gh-we":"dfvndkssa"},"shortSlugOption":{"jk-cv":"sdocjosmdc"},"mediaFileAssetLink":{"en-us":"jdsojocis"},"booleanField":{"cb-af":"true"}}},{"system":{"id":"2eAmIIuG4xkLvatkU3RUSy","type":"Entry","name":"dummy"},"DataDetails":{"dummy":{"en-us":"dshcifdvk"},"india":{"kh-vh-we":"sdci"}}},{"system":{"id":"7hbdS3MgfZ73TOtlu1WfXw","type":"Entry","name":"dummy"},"DataDetails":{"testingNewValue":{"ax-lo":"sdcoklsdc"},"locationField":{"sd-op":"sdcndkdc"}}}]

let res = data.reduce((acc,curr) => {
    if(!acc[curr.system.name]){
    acc[curr.system.name] = {}
  }
  
  let detailsObj = {}
Object.keys(curr.DataDetails).forEach((detail) => {
    detailsObj[detail] = Object.values(curr.DataDetails[detail])[0]
})
  
  acc[curr.system.name][curr.system.id] = {
    title:curr.system.id,
    uid:curr.system.id,
        url:`/${curr.system.name}/${curr.system.id}`,
    ...detailsObj
  }
  return acc;
},{})

console.log(res)

The result of a specific name can be accessed as res['Author'] for example

Sign up to request clarification or add additional context in comments.

1 Comment

yes perfect :D its thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.