Reputation: 473
I'm trying to convert my date string into an array.
Date string: var date = "2017,03,23";
Desired result: [2017,03,23]
Here is what I tried:
var new_date = date.split(','); // result: ["2017", "03", "23"]
I want [2017,03,23]
.
How do I do this?
Upvotes: 2
Views: 3920
Reputation: 505
Better and short way would be using
var date ="2017,03,23";
var output = date.split(',').map(Number);
console.log(output);
Upvotes: 1
Reputation: 56393
this should do:
var date = "2017,03,23";
var array = date.split(",").map(Number);
console.log(array);
Upvotes: 3
Reputation: 5275
You're getting an array of String
, instead of an array of int
. You just need to convert the array you have into int
s in a new array. Here's an example, using parseInt()
:
var new_date = date.split(',');
for(i = 0; i < new_date.length; i++){
new_date[i] = parseInt(new_date[i]);
Upvotes: 1
Reputation: 1606
You can parseInt
the array
!
var date = "2017,03,23";
date = date.split(',');
for(var i=0; i<date.length; i++) { date[i] = parseInt(date[i], 10); }
console.log(date);
Upvotes: 1