3

So I am learning JavaScript/jQuery and I need some help.

Basically I have a array, converted from an Excel table below

Name    Sun  Mon    Tues    Wed     Thurs   Fri    Sat
John    x   21:00   21:00   21:00   21:00   21:00   x
Smith   x   19:45   19:45   19:45   19:45   19:45   x
Paul    x   19:45   19:45   19:45   19:45   19:45   11:00
"Name", "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", 
"John", "x", "21:00", "21:00", "21:00", "21:00", "21:00", "x", 
"Smith", "x", "19:45", "19:45", "19:45", "19:45", "19:45", "x", 
"Paul", "x", "19:45", "19:45", "19:45", "19:45", "19:45", "11:00"

What I want:

A function when given a input of "John, Monday" or "John, 27/02/2017", will return "21:00".

Any sample code is highly appreciated, as I am fairly new to this.

5
  • 1
    Err... So is your question "How do I convert a date to a day of the week?" Commented Feb 27, 2017 at 2:00
  • haha no sorry i will edit the question, i might have added too much, My question is how do i get the value for the input, if i put "John" and the date, the function should return the time from the row associated with the name, John, mon should return 21:00 Commented Feb 27, 2017 at 2:48
  • How do you know the data starts from 27 feb 2017? or do you just get the week day and get the value from your array? Commented Feb 27, 2017 at 3:18
  • @Jules let me explain, originally i had a excel table which i later just converted to array for my javascript, the data does not have any dates, only days, Monday, Tuesday so on as columns , the timings are fixed aswell, now the user will be entering his name and a random date(27/02/2017) fixed format, the only purpose of date is to convert to day then the function checks his name against the day and tells the time, Like "John Teague" answers which works, but the table has 100's of rows , so i used a online converter to convert the data in my example, so his method requires manual work too. Commented Feb 27, 2017 at 3:31
  • Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented Feb 27, 2017 at 21:54

2 Answers 2

1

I think it would be easier if you restructure your data.

const data = {
  John: [
      'x',
      '21:00',
      '21:00',
      '21:00',
      '21:00',
      '21:00',
      'x',
  ],
  Smith: [
    'x',
    '19:45',
     '19:45',
    '19:45',
    '19:45',
    '19:45',
    'x',
  ],
  Paul: [
    'x',
    '19:45',
    '19:45',
    '19:45',
    '19:45',
    '19:45',
    '11:00',
  ],
}

The array is for the values for each day, since Date.getDay() return 0..6 based on the day. (0 = Sunday, etc).

In JavaScript objects can be treated as a dictionary and you can access them using [] notation.

const name = 'John';
const dayIndex = new Date('02/28/2017').getDay();
const result = data[name][dayIndex]; // result = 21:00
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that you can convert the data row to array (note I changed the minutes for testing)

var dat = ["Name", "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", 
"John", "x", "21:00", "21:00", "21:00", "21:00", "21:00", "x", 
"Smith", "x", "19:45", "19:45", "19:45", "19:45", "19:45", "x", 
"Paul", "x", "19:45", "19:45", "19:45", "19:45", "19:45", "11:00"];

var data = [];

for (var i = 1; i < dat.length / 8; i++) {
  data.push(dat.slice(i * 8, (i + 1) * 8));
}

function getIt(name, date) {
  var el = data.find(function(e) {
    return e[0] == name;
  });

  if (el) {
    console.log(el[date.getDay() + 1]);
  } else {
    console.log("not found");
  }
}

getIt("John", new Date());

EDIT: added parsing string to data source array.

2 Comments

Hey jules, i tried your code but i cant seem to make it work, different errors keep popping up, i will try my best to fix it but, could you take a another look ?, i am using jsfiddle by the way.
@Cyb3rHac3r, what is the error message? Can you share your jsfiddle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.