Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
creando un objeto anidado personalizado agregando una clave en javascript / node js

 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

  1. Autor
  2. en-us/zo-ap / otra clave que está disponible en obj
  3. 4gSSbjCFEorYXqrgDIP2FA/ 1aBOO8tu3lUsjticuIbUM5
  4. luego el objeto json restante

Rendimiento esperado

 { "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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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; }

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!