1

I have this function:

const dateFormatUsEnglish = new new Intl.DateTimeFormat("en-US");
const debug = dateFormatUsEnglish.formatToParts(base);


// return value: 
[ 
  { type: 'weekday',   value: 'Monday' }, 
  { type: 'literal',   value: ', '     }, 
  { type: 'month',     value: '12'     }, 
  { type: 'literal',   value: '/'      }, 
  { type: 'day',       value: '17'     }, 
  { type: 'literal',   value: '/'      }, 
  { type: 'year',      value: '2012'   }, 
  { type: 'literal',   value: ', '     }, 
  { type: 'hour',      value: '3'      }, 
  { type: 'literal',   value: ':'      }, 
  { type: 'minute',    value: '00'     }, 
  { type: 'literal',   value: ':'      }, 
  { type: 'second',    value: '42'     }, 
  { type: 'fractionalSecond', value: '000' },
  { type: 'literal',   value: ' '      }, 
  { type: 'dayPeriod', value: 'AM'     } 
]

Now I wanna write if statements like "if debug[2].month.value === x..." in order to replace / correct the user's input live. How can I manage this?

1 Answer 1

1

You can transform result Array to Mapped Object with

const mapped = result.reduce((accumulator, currentValue) => { 
    accumulator[currentValue.type] = currentValue.value 
    return accumulator
}, {})

Now you can access with mapped.month and have 12 value

Sign up to request clarification or add additional context in comments.

1 Comment

In my case debug[x].value where x points to the arr cell I could get my value, but your answer is nice!

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.