【问题标题】:Calling array values based on DOM return根据 DOM 返回调用数组值
【发布时间】:2019-06-01 04:31:53
【问题描述】:

根据拉取的 dom 元素返回一个字符串

我有一个对象存储月份及其索引(不是日期)monthList = {"jan" : "1", "feb" : "2". etc: etc}

用户可以键入类似 jan 或 jan,feb,march 之类的内容,我希望将字符串返回为 1 或 1,2,3(我用于其他地方的 get 调用),但无法执行此操作,我尝试使用各种疯狂的循环都无济于事,从错误读取到始终读取最后一个索引,如果用户输入 1,2,3 也应该有效。

使用${document.getElementById('monthInput').value} 简单调用的输入值

User Input Example #1: jan,feb,dec
User Input Output #1: 1,2,12
User Input Example #2: 1,2,5
User Input Output #2: 1,2,5

我该怎么做?

【问题讨论】:

    标签: javascript html loops


    【解决方案1】:

    我承认不明白为什么你会接受这种奇怪格式的用户输入。转换不匹配特定语法/格式的用户输入的唯一方法是使用手动创建的矩阵。

    在下面的inputMap 中,您需要列出每个用户输入及其应转换为的字符串值:

    const inputMap = {
      "jan": "1",
      "feb": "2",
      "march": "3",
      1: "1",
      2: "2",
      3: "3",
      "dec": 12,
      5: "5"
    }
    
    const input1 = "jan,feb,dec"
    const input2 = "1,2,5"
    
    const mapInput = inputString => inputString.split(",").map(i => inputMap[i]).join(",")
    
    console.log(mapInput(input1))
    console.log(mapInput(input2))

    【讨论】:

    • 是否有办法检测逗号后的偶发空格,比如Jan, 3,Jun,7, Dec 变为 1,3,6,7,12?
    • 我可以用空间元素复制输入地图,但这似乎是不切实际的复制。比如我有Foo a Bar, Foobar, foo,fooBar作为输入,输出将是Foo a Bar,Foobar,foo,fooBar作为输出
    • 你肯定可以用 inputMap[i.trim()]
    【解决方案2】:

    根据this answer,你可以递归地使用Date来完成这个,给定输入months

    months.split(",").map((month) => {
      return (isNaN(month) ? new Date(Date.parse(month +" 1, 2019")).getMonth()+1 : month)
    }).join(",")
    

    此函数使用map 遍历每个代码/数字字符串,在三元运算符中使用isNaN() 检查给定字符串是否不是数字,并相应地返回给定数字/转换后的代码。

    【讨论】:

      【解决方案3】:

      您可以通过以下几种方式做到这一点:

      1. 使用简单的for..of 循环
      2. 使用.replace()(保持原字符串的格式)
      3. 使用映射方法(使用:.map
      4. 过度使用递归 + 三元...

      1. 使用循环:

      const months = {jan:"1",feb:"2",mar:"3",apr:"4",may:"5",jun:"6",jul:"7",aug:"8",sep:"9",oct:"10",nov:"11",dec:"12"};
      
      const input = "jan,dec,feb";
      const dates = input.split(','); // turn your input into an array
      
      let converted = "";
      for(let month of dates) { // loop through each month in dates
        if(month in months) { // check if the month is a key in months
          converted += months[month] +','; // add number version to converted sring
        } else { // if the month isn't in the converted month object, then no need to convert it
          converted += month+','; // add month to (ie the number) to the converted output
        }
      }
      
      console.log(converted.slice(0, -1)); // using slice(0, -1) to remove trailing comma
      1. 使用.replace() 保持原始格式:

      const months = {jan:"1",feb:"2",mar:"3",apr:"4",may:"5",jun:"6",jul:"7",aug:"8",sep:"9",oct:"10",nov:"11",dec:"12"};
      
      let input = "jan,   dec,  feb, 5";
      const dates = input.split(','); // turn your input into an array
      
      for(let month of dates) {
        month = month.trim();
        if(month in months) {
          input = input.replace(month, months[month]);
        }
      }
      
      console.log(input);
      1. 使用map。这里为每个month 调用内部箭头函数,然后将其转换为months 对象中的关联值。然后我们使用.join(',') 加入值数组:

      const months = {jan:"1",feb:"2",mar:"3",apr:"4",may:"5",jun:"6",jul:"7",aug:"8",sep:"9",oct:"10",nov:"11",dec:"12"};
      const input = "jan,dec,feb";
      
      const converted = input.split(',')
                             .map((month) => months[month] || month)
                             .join(',');                    
      console.log(converted);
      1. 对三元运算符使用递归:

      const months={jan:"1",feb:"2",mar:"3",apr:"4",may:"5",jun:"6",jul:"7",aug:"8",sep:"9",oct:"10",nov:"11",dec:"12"};
      const input = "jan,dec,feb";
      
      const res = (f = ([m, ...rest]) => m && m in months ? months[m]+','+f(rest) : m ? m+','+f(rest) : '')(input.split(',')).slice(0,-1);
      console.log(res);

      【讨论】:

        猜你喜欢
        • 2018-05-17
        • 1970-01-01
        • 2018-03-17
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多