Adrian Sultu
Adrian Sultu

Reputation: 350

How can i avoid overwriting the object propriety every time with this loop?

I have the code below that parses an Excel file, get's the user's schedule and add it to an object. If i use an array i can push individual date objects with the start and end of the shift. But if i want to use an object instead and have the dates be individual proprieties of that object, then it get overwritten every time it goes through the loop. And i way i can make this work?

const ExcelJS = require("exceljs");
let date = new Date(2020, 05, 01);
var moment = require("moment");
const schedules = {
  "user1": [],
};
shifts = {
  "9-18": {
    start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("18:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "12-21": {
    start: moment("12:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "10-14": {
    start: moment("10:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("14:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "9-16": {
    start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("116:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "15-21": {
    start: moment("15:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "21-1": {
    start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("1:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "21-6": {
    start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("6:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  "00-9": {
    start: moment("00:00:00", "HH:mm:ss").format("hh:mm A"),
    end: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
  },
  DO: { start: null, end: null },
  CO: { start: null, end: null },
};

const readxl = async () => {
  const workbook = new ExcelJS.Workbook();
  const file = await workbook.xlsx.readFile("../../June.xlsx");
  const worksheet = file.getWorksheet("June");
  let cell = 5;
  let row = 9;
  let name = 4;

  while (true) {
    let rows = worksheet.getRow(row);
    if (row === 10) {
      break;
    } else if (row === 19) {
      row = 28;
    } else if (row === 31) {
      row = 33;
    } else if (cell === 35) {
      date.setDate(date.getDate() - 30);
      row += 1;
      console.log(row);
      cell = 5;
    }

    while (true) {
      if (cell === 35) {
        break;
      }
      //   console.log(
      //     date.toLocaleDateString(),
      //     rows.getCell(name).value,
      //     `${shifts[rows.getCell(cell).value].start} - ${
      //       shifts[rows.getCell(cell).value].end
      //     }`
      //   );
      schedules[rows.getCell(name).value].push({
        [date.toLocaleDateString()]: {
          start: shifts[rows.getCell(cell).value].start,
          end: shifts[rows.getCell(cell).value].end,
        },
      });
      cell++;
      date.setDate(date.getDate() + 1);
    }
  }
  console.log(schedules["user1"]);
};

readxl();

Upvotes: 0

Views: 44

Answers (1)

Tridibesh Nag
Tridibesh Nag

Reputation: 52

Just have an array for each date. Objects cannot have multiple similar keys.

if(!evelina[rows.getCell(name).value]) {
    evelina[rows.getCell(name).value] = [];
}

if(!evelina[rows.getCell(name).value][date.toLocaleDateString()]) {
    evelina[rows.getCell(name).value][date.toLocaleDateString()]=[];
}

evelina[rows.getCell(name).value][date.toLocaleDateString()].push({
          start: shifts[rows.getCell(cell).value].start,
          end: shifts[rows.getCell(cell).value].end,
        });

Upvotes: 1

Related Questions