3 Question: Calling array values based on DOM return

question created at Sat, Jun 1, 2019 12:00 AM

Return a string based on pulled dom elements

I have an object storing months and their index (not dates) monthList = {"jan" : "1", "feb" : "2". etc: etc}

The user can type something like jan or jan,feb,march and I want a string returned as 1 or 1,2,3 (which I use for a get call elsewhere) but haven't been able to do this, ive tried using all sorts of crazy loops to no avail, from incorrect reading to always reading last index, if the user inputs 1,2,3 that should also work.

The input values as simply called using ${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

How can I do this?

3 Answers 3

I admit to not understanding why you would accept user input in such odd formats. The only way to transform user input that matches no specific syntax/format is to use a manually created matrix.

In the inputMap below you would need to list each user input and the string value that it should be translated to:

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(",")

2019-06-01 05:20:04Z
  1. would there be a way to make that detect the occasional space after a comma, say Jan, 3,Jun,7, Dec become 1,3,6,7,12?
    2019-06-01 05:34:36Z
  2. i could duplicate the input map with space elements, but that seems like impractical duplication. Like for example I have Foo a Bar, Foobar, foo,fooBar as the input, output would be Foo a Bar,Foobar,foo,fooBar as the output
    2019-06-01 06:13:12Z
  3. You sure could with inputMap[i.trim()]
    2019-06-01 06:35:20Z

According to this answer, you can recursively use Date to accomplish this, given input months:

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

This function iterates over each code/number string using map, checks whether or not the a given string is not number using isNaN() in a ternary operator, and accordingly returns the given number/converted code.

2019-06-01 04:53:17Z

You can do this a few ways:

  1. Using a simple for..of loop
  2. Using .replace() (keeps the formatting of original string)
  3. Using a mapping method (eg: .map
  4. Going overboard with recursion + ternaries...

  1. Using Loops:

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. Using .replace() to keep original formatting:

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]);

  1. Using map. Here the inner arrow function is called for each month, and then converted to its associated value in the months object. We then use .join(',') to join the array of values:

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) => month in months ? months[month] : month)
  1. Using recursion with the ternary operator:

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);
2019-06-01 05:50:09Z
source placed here