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

104
Views
cómo insertar profundamente en el objeto

Tengo este objeto con tablas:

 let tables = { "2021-08-25": { "B0705": { "48": "heaven" }, "B0704": { "48": "hell" } } }

Y me gustaría insertar dinámicamente una nueva tabla. Intenté esto:

 var insertTable = (date,table,slot) => { let newDesk = { [date]: { [table]: { [slot]: 'slotter' } } }; Object.assign(tables,newDesk); };

Pero sobrescribe mis entradas existentes.

Esto tampoco funcionará:

 var insertTable2 = (date,table,slot) => { Object.defineProperty(tables, date, { table: {slot: 'slotter'} }); };

¿Cómo funcionará esto?

 insertTable2("2021-08-25","B0705","22"); insertTable2("2021-08-25","B0705","12"); insertTable2("2021-08-25","B0706","33"); insertTable2("2021-08-26","B0703","11");
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para una solución más reutilizable, eche un vistazo a esta pregunta SO : desea fusionar en profundidad el nuevo objeto con el anterior, no fusionarlos superficialmente.

Se puede construir una solución no reutilizable de esta manera (para ilustrar que para rutas/requisitos más complejos, la solución anterior es mucho más fácil):

 let tables = { "2021-08-25": { "B0705": { "48": "heaven" }, "B0704": { "48": "hell" } } } // this will mutate the tables object const insertTable = (date, table, slot) => { // is there an object with this date yet? if not create it. if(typeof tables[date] === "undefined"){ tables[date] = {}; } // is there an object with this table yet? if not create it. if(typeof tables[date][table] === "undefined"){ tables[date][table] = {}; } // set the value now that we know all intermediate objects are defined. tables[date][table][slot] = "slotter"; } console.log(tables); insertTable("2021-08-25","B0705","22"); insertTable("2021-08-25","B0705","12"); insertTable("2021-08-25","B0706","33"); insertTable("2021-08-26","B0703","11"); console.log(tables);

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar una matriz para expresar la ruta del objeto en la que desea que se coloque el valor, especificando también el valor que se establecerá en la ruta.

Luego Array.reduce() para navegar por la ruta y eventualmente establecer el value en el último elemento de la ruta:

 let tables = { "2021-08-25": { "B0705": { "48": "heaven" }, "B0704": { "48": "hell" } } } function insertTable(table, path, value) { path.reduce((acc, pathElement, idx) => { if (idx < path.length -1) { // We're not at the final element of path, add a new object if necessary if (!acc[pathElement]) acc[pathElement] = {}; } else { // We're at the last pathElement, set the value, eg 'slotter' acc[pathElement] = value; } return acc[pathElement]; }, table) } insertTable(tables, ['2021-08-25', 'B0705', '22'], 'slotter'); insertTable(tables, ['2021-08-25', 'B0705', '12'], 'slotter'); insertTable(tables, ['2021-08-25', 'B0706', '33'], 'some value'); insertTable(tables, ['2021-08-26', 'B0703', '11'], 'another value'); console.log(tables)

about 4 years ago · Juan Pablo Isaza 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!