Home > Back-end >  How do I destructure strings in JS using a special character? [duplicate]
How do I destructure strings in JS using a special character? [duplicate]

Time:09-27

I want day,month and year values from the value in an object.

{myDate : '12/31/2020'}

I want to do something like this:

d,m,y = mydate('/')

so that d= 12, m=31 , y = 2020 and they are separated using the '/' character

Is there a way to do something like this in JS?

CodePudding user response:

Split the string and destructure the resulting array:

const myDate = {myDate : '12/31/2020'}
const [d,m,y] = myDate.myDate.split('/')
console.log(d,m,y)

CodePudding user response:

const [d,m,y] = '12/31/2020'.split("/")

console.log(d,m,y)

CodePudding user response:

Use

mydate = "12/30/2021";
var arr = mydate.split("/");
//arr[0]=12
//arr[1] is day
  • Related