0

I have an string array as follows in javsacript:

var dateArray=
["2014,01,01","2014,07,04","2014,09,01","2014,11,25"]

I want to convert this array as follows:

[2014,01,01],[2014,07,04],[2014,09,01],[2014,11,25]

Above arrays will be passed to datepicker library to disable few dates. Can anyone please help me in achieving this.

4
  • It is simple, loop through the initial array,and use a split function. Push each of the split list into another array Commented Nov 16, 2014 at 4:11
  • @Anand: Try writing some code for this, we will guide and help you. Commented Nov 16, 2014 at 4:13
  • @Aravind I am trying something like this. [5,6,[2014,5,9],[2014,9,6]].In the above snippet, input will be [5,6].I want to add [2014,5,9] & [2014,9,6] to it.Its type is number not string Commented Nov 16, 2014 at 4:27
  • @Anand: Though it's easy to get answers here, it won't help you learn, unless you try. While it is good to ask questions, make sure that you understand how each answer works, so you can use them in your future. Ask questions in comments if you don't understand how their answer works. Instead of saying it does not work for this input, find out why it does not work, and fix it. That way, you learn how it works. Commented Nov 16, 2014 at 6:43

3 Answers 3

2

Use the array map function:

dateArray.map( function(x) { 
    return x.split( ',' );
} );
Sign up to request clarification or add additional context in comments.

3 Comments

Quite precise! And x.split( ',' ).map(function(y) { return (+y); }); would convert the strings to numeric values.
@PeterKA I already have an array like [5,6]. I have another array like ["2014,5,6","2014,5,9"].Now I want output like [5,6,[2014,5,6],[2014,5,9]]. Output array is of type number.
@Anand—arrays are Type object. The members of the array might have a Type of number. ;-)
1

var dateArray=
["2014,01,01","2014,07,04","2014,09,01","2014,11,25"];

var y = dateArray.map( function(x) { 
    return x.split( ',' ).map(function(v) { return (+v); });
});

console.log( y );

UPDATE

var a = [5,6],
    b = ["2014,5,6","2014,5,9"];

  
b.forEach( function(x) { 
    a.push( x.split( ',' ).map(function(v) { return (+v); }) );
});


console.log( a );

1 Comment

Thanks for the help. If i run this snippet a.length gives me 3. I am expecting 4 here [5, 6 , [2014,5,6] , [2014,5,9] ].I have tried Peter but to no avail. Help is highly appreciated.
0

 var dateArray =
      ["2014,01,01", "2014,07,04", "2014,09,01", "2014,11,25"];
    const getdatearr = dateArray.map(x => {
      return x.split(' ')
    });
    console.log('getdarearr', getdatearr);

  

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.