How do I destructure strings in JS using a special character? [duplicate] - javascript

This question already has answers here:
Separate string into multiple variables with JavaScript
(3 answers)
Split a string straight into variables
(4 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
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?

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)

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

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

Related

How to make an array by one string in JS [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 2 years ago.
I get one string by query like '5e6,5e4,123'.
And I want to make an array containing this query as below in JS.
['5e6', '5e4', '123']
How can I make this? Thank you so much for reading it.
You can use .split(',')
var str = "5e6,5e4,123";
var array = str.split(',');
console.log(array);
You can read more on this here
Use String.split:
console.log('5e6,5e4,123'.split(","))
var query = '5e6,5e4,123';
var queries = query.split(‘,’);
You can make use of split method of string like below:
var res = str.split(',');
const output = input.split(',');

i am unable to get specific regex [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&
i have a string like this and i trying to get specific value from it, it is in text format. what can i do if i want to get value of 'aa' it null and value of 'bb' it is 0108135719 tats it. I'm tried different regex but unable to get the desired output.
You can do something like
var t="aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&".split("&")
console.log(t)
var ansObj = {}
t.forEach((element) => {
const elementArray = element.split("=")
const key = elementArray[0]
const value = elementArray[1]
ansObj[key] = value
})
console.log(ansObj)

Convert JSON /Date(1238626800000)/ to Unix Timestamp [duplicate]

This question already has answers here:
How to parse JSON to receive a Date object in JavaScript?
(17 answers)
Closed 5 years ago.
In my code I get JSON response as /Date(1238626800000)/.
I want to convert this object to Unix Timestamp. So I would like to know that whether is there any default javascript or jquery method which can convert it to Unix Timestamp ?
So My Input Date is: /Date(1238626800000)/ and
Output I want is: 1238626800000
I can do it with RegEx but this is last option if no default method available
No need to use regex here. Just slice out the timestamp:
if (value.startsWith("/Date(") && value.endsWith(")/"))
return new Date(Number(value.slice(6, -2)));
like this:
var input = '/Date(1238626800000)/';
var re = /Date\(([0-9]*)\)/;
var ret = re.exec(a);
if(ret) {
input = ret[1];
}

Required a solution in searching string in NodeJs [duplicate]

This question already has answers here:
Use dynamic (variable) string as regex pattern in JavaScript
(8 answers)
Closed 5 years ago.
I have a string "platform:17.01.02" and I want to search for "17.01.02" which I'm giving through a variable.
a = "platform:17.01.02"
b = "17.01.02" (has to taken from user)
a.search(/b/)
The above statement search for "b" and not for variable value "b". Can any one help how can i search that?
Try this:
var a = "platform:17.01.02";
var b = "17.01.02";
console.log(a.search(new RegExp(b))); //9
If you just want to know if a contains b, use:
a.indexOf(b) >= 0

How to change a plain text separated with comma into an array in node? [duplicate]

This question already has answers here:
Javascript Equivalent to PHP Explode()
(17 answers)
Closed 6 years ago.
I have this text as my post tags:
car,phone,apple,node,php
and I wanna convert that into an array, like this:
["car","phone","apple","node","php"]
and then save that into my mongodb database.
how can I do that in my server.js code?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
passing , as an argument to split, will do it for you.
> x = "car,phone,apple,node,php"
'car,phone,apple,node,php'
> x.split(",")
[ 'car', 'phone', 'apple', 'node', 'php' ]
"car,phone,apple,node,php".split(',');
will do the thing
Use javascript default split function.
var stringVar = "car,phone,apple,node,php";
console.log(stringVar.split(','));
var str ="car,phone,apple,node,php";
var arr =str.split(',');
console.log(arr);
split() function converts the string into array.

Categories