javascript - 匹配数组和返回值

标签 javascript jquery arrays

所以我正在学习 JavaScript/jQuery,我需要一些帮助。

基本上我有一个数组,从下面的 Excel 表格转换而来

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"

我想要的:

当函数输入“John, Monday”或“John, 27/02/2017”时,将返回“21:00”。

非常感谢任何示例代码,因为我对此还很陌生。

最佳答案

我认为如果您重组数据会更容易。

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',
  ],
}

该数组用于存储每一天的值,因为 Date.getDay() 会根据日期返回 0..6。 (0 = 星期日等)。

在 JavaScript 中,对象可以被视为字典,您可以使用 [] 符号访问它们。

const name = 'John';
const dayIndex = new Date('02/28/2017').getDay();
const result = data[name][dayIndex]; // result = 21:00

关于javascript - 匹配数组和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476547/

相关文章:

javascript - Onmouseout 事件不起作用

javascript - 将动画添加到下拉菜单

javascript - 在 jQuery 中使用模型中的 bool

python - Django ArrayField 追加,避免竞争条件

mysql - 从 1D JSON 数组中搜索/提取值

javascript - 使用javascript更改表单内表单中输入文本的值

javascript - 无法使用 jquery POST 查询 Azure 搜索

javascript - 单击任意位置关闭 javascript 弹出窗口

javascript - 隐藏事件的点击事件然后重新出现

javascript - 如何创建二维数组数组?