Skip to content
Advertisement

how to insert deep in object

I have this object with tables:

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

An I would like to insert dynamicly a new table. I tried this:

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

But it overwrites my exsiting entrances.

This will also not work:

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

How this will work?

insertTable2("2021-08-25","B0705","22");
insertTable2("2021-08-25","B0705","12");
insertTable2("2021-08-25","B0706","33");
insertTable2("2021-08-26","B0703","11");

Advertisement

Answer

For a more reusable solution, take a look at this SO question – you want to deep merge the new object with the old one, not shallowly merge them.

A non reusable solution can be built like this (to illustrate that for more complex paths/requirements the above solution is much easier):

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);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement