let data = [ { system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", content: { type: { name: "Author", }, }, }, DataDetails: { shortSlugOption: { "en-us": "some value", "za-op": "random value" }, mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" }, mediaFileAssetGalary: { hi: "file link 2" }, singleMediaImage: { hi: "file link single", "en-us": "english link" }, }, }, { system: { id: "1aBOO8tu3lUsjtICuIbUM5", type: "Entry", content: { type: { name: "Author", }, }, }, DataDetails: { short: { "en-us": "details of shorts", "za-op": "short details" }, shortSlugOption: { "hi-In": "options" }, booleanField: { "hi-In": "true" }, }, }, { system: { id: "2pOUGnI1oRD7nsrYs600HA", type: "Entry", content: { type: { name: "testing", }, }, }, DataDetails: { testingNewValue: { "en-us": "details value" } }, }, { system: { id: "66rzYr2BpWL1VTBHdLTdSW", type: "Entry", content: { type: { name: "new", }, }, }, DataDetails: { oneReference: { hi: "values 1" }, multiReference: { "hi-In": "values 2" }, }, }, { system: { id: "cIb5mqEBRWDD6hrNmFmFE", type: "Entry", content: { type: { name: "new", }, }, }, DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } }, }, { system: { id: "7kRzyt4PFrX13gHcw3Z1Ko", type: "Entry", content: { type: { name: "testing", }, }, }, DataDetails: { testingNewValue: { "en-us": "kknksdo" } }, }, { system: { id: "2OspeCtNK0sh2cPiuU9jIz", type: "Entry", content: { type: { name: "dummy", }, }, }, DataDetails: { short: { "za-op": "dfvndkssa" }, shortSlugOption: { hi: "sdocjosmdc" }, mediaFileAssetLink: { "en-us": "jdsojocis" }, booleanField: { "hi-In": "true" }, }, }, { system: { id: "2eAmIIuG4xkLvatkU3RUSy", type: "Entry", content: { type: { name: "dummy", }, }, }, DataDetails: { dummy: { "en-us": "dshcifdvk" }, india: { "za-op": "sdci", hi: "hewd" }, }, }, { system: { id: "7hbdS3MgfZ73TOtlu1WfXw", type: "Entry", content: { type: { name: "dummy", }, }, }, DataDetails: { testingNewValue: { "en-us": "sdcoklsdc" }, locationField: { hi: "sdcndkdc" }, }, }, ]; let res = data.reduce((acc, curr) => { if (!acc[curr.system.content.type.name]) { acc[curr.system.content.type.name] = {}; } let detailsObj = {}; Object.keys(curr.DataDetails).forEach((detail) => { detailsObj[detail] = Object.values(curr.DataDetails[detail])[0]; }); acc[curr.system.content.type.name][curr.system.id] = { ...detailsObj, }; return acc; }, {}); console.log(res);Estoy almacenando mis datos en un archivo JSON pero haciendo algunas modificaciones en mi objeto actual.
los datos que quiero almacenar en JSON son así
Me gusta Datos presentes
{ "Author": { "en-us": { "4gSSbjCFEorYXqrgDIP2FA": { "shortSlugOption": "some value", "mediaFileAssetLink": "some file", "singleMediaImage": "english link" }, "1aBOO8tu3lUsjtICuIbUM5": { "short":"details of shorts" } }, "za-op": { "4gSSbjCFEorYXqrgDIP2FA": { "shortSlugOption": "random value", "mediaFileAssetLink":"file Linl", }, "1aBOO8tu3lUsjtICuIbUM5": { "short":"short details", } }, "hi": { "4gSSbjCFEorYXqrgDIP2FA": { "mediaFileAssetGalary": "file link 2", "singleMediaImage":"file link single" } }, "hi-In": { "1aBOO8tu3lUsjtICuIbUM5": { "shortSlugOption": "options", "booleanField":true } } }, "dummy": { "en-us": { "2OspeCtNK0sh2cPiuU9jIz": { "mediaFileAssetLink":"jdsojocis", }, "2eAmIIuG4xkLvatkU3RUSy": { "dummy": "dshcifdvk" } "7hbdS3MgfZ73TOtlu1WfXw": { "testingNewValue":"sdcoklsdc" } }, "za-op": { "2OspeCtNK0sh2cPiuU9jIz": { "short":"dfvndkssa", }, "2eAmIIuG4xkLvatkU3RUSy": { "india":"sdci" } }, "hi": { "2OspeCtNK0sh2cPiuU9jIz": { "shortSlugOption":"sdocjosmdc" }, "2eAmIIuG4xkLvatkU3RUSy": { "india":"hewd" } } "hi-In": { "2OspeCtNK0sh2cPiuU9jIz": { "booleanField":"true" }, "7hbdS3MgfZ73TOtlu1WfXw": { "locationField":"sdcndkdc" } } }, "new": { "hi": { "66rzYr2BpWL1VTBHdLTdSW": { "oneReference":"values 1" } }, "hi-In": { "66rzYr2BpWL1VTBHdLTdSW": { "multiReference": "values 2" , }, "hi-IN": { "cIb5mqEBRWDD6hrNmFmFE": { "testingNewValue":"jksdsdo" } } } }. "testing": { "en-us": { "2pOUGnI1oRD7nsrYs600HA": { "testingNewValue":"details value" }, "7kRzyt4PFrX13gHcw3Z1Ko": { "testingNewValue":"kknksdo" } }, } }Como en el código anterior, se eliminan en-us hi hi-In hi-IN zo-ap o cualquier otra tecla
pero quiero crear un objeto json anidado con este nombre o cualquier otro nombre que esté presente dentro de json
Necesita las entradas anidadas y usa las claves en orden cambiado.
const data = [{ system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", content: { type: { name: "Author" } } }, DataDetails: { shortSlugOption: { "en-us": "some value", "za-op": "random value" }, mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" }, mediaFileAssetGalary: { hi: "file link 2" }, singleMediaImage: { hi: "file link single", "en-us": "english link" } } }, { system: { id: "1aBOO8tu3lUsjtICuIbUM5", type: "Entry", content: { type: { name: "Author" } } }, DataDetails: { short: { "en-us": "details of shorts", "za-op": "short details" }, shortSlugOption: { "hi-In": "options" }, booleanField: { "hi-In": "true" } } }, { system: { id: "2pOUGnI1oRD7nsrYs600HA", type: "Entry", content: { type: { name: "testing" } } }, DataDetails: { testingNewValue: { "en-us": "details value" } } }, { system: { id: "66rzYr2BpWL1VTBHdLTdSW", type: "Entry", content: { type: { name: "new" } } }, DataDetails: { oneReference: { hi: "values 1" }, multiReference: { "hi-In": "values 2" } } }, { system: { id: "cIb5mqEBRWDD6hrNmFmFE", type: "Entry", content: { type: { name: "new" } } }, DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } } }, { system: { id: "7kRzyt4PFrX13gHcw3Z1Ko", type: "Entry", content: { type: { name: "testing" } } }, DataDetails: { testingNewValue: { "en-us": "kknksdo" } } }, { system: { id: "2OspeCtNK0sh2cPiuU9jIz", type: "Entry", content: { type: { name: "dummy" } } }, DataDetails: { short: { "za-op": "dfvndkssa" }, shortSlugOption: { hi: "sdocjosmdc" }, mediaFileAssetLink: { "en-us": "jdsojocis" }, booleanField: { "hi-In": "true" } } }, { system: { id: "2eAmIIuG4xkLvatkU3RUSy", type: "Entry", content: { type: { name: "dummy" } } }, DataDetails: { dummy: { "en-us": "dshcifdvk" }, india: { "za-op": "sdci", hi: "hewd" } } }, { system: { id: "7hbdS3MgfZ73TOtlu1WfXw", type: "Entry", content: { type: { name: "dummy" } } }, DataDetails: { testingNewValue: { "en-us": "sdcoklsdc" }, locationField: { hi: "sdcndkdc" } } }], result = data.reduce((r, { system: { id, content: { type: { name } } }, DataDetails }) => { r[name] ??= {}; Object.entries(DataDetails).forEach(([k, o]) => { Object.entries(o).forEach(([l, v]) => { r[name][l] ??= {}; r[name][l][id] ??= {}; r[name][l][id][k] = v; }); }); return r; }, {}); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }