0

I have an AJAX response like this

"12-02-2017,12-02-2017,11-02-2017,10-02-2017"

and i want convert this response into

   var sortDates =["12-02-2017","18-02-2017","11-02-2017","10-02-2017"];

Any questions, please comment.

Reinstate Monica Cellio
  • 25,420
  • 6
  • 49
  • 67
  • [`String.prototype.split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) – Pointy Jan 30 '17 at 15:20
  • can you help me sort list of dates and implode array Array ( [0] => 10-02-2017 [1] => 11-02-2017 [2] => 12-02-2017 [3] => 04-02-2017 ) array is like this using php?? – Muhammad Awais Zulifqar Jan 30 '17 at 15:45

1 Answers1

4

You can use split():

var resp = "12-02-2017,12-02-2017,11-02-2017,10-02-2017";
resp = resp.split(',');
console.log(resp);
Ionut
  • 10,707
  • 4
  • 40
  • 69
  • can you help me sort list of dates and implode array Array ( [0] => 10-02-2017 [1] => 11-02-2017 [2] => 12-02-2017 [3] => 04-02-2017 ) array is like this using php?? – Muhammad Awais Zulifqar Jan 30 '17 at 15:45
  • @MuhammadAwaisZulifqar, how do you want to be sorted? Ascending or descending? – Ionut Jan 30 '17 at 15:52
  • Assuming you have the array in a variable called `$dates` you can do the next thing: `usort($dates, "compare"); function compare($a, $b){ return strcmp($a, $b); }`. To order them descending just switch the order of `$a` and `$b` in the `strcmp()` function. – Ionut Jan 30 '17 at 16:04
  • @MuhammadAwaisZulifqar, then you can just `implode` the array by `,` let's say like `$dates = implode(',', $dates);` – Ionut Jan 30 '17 at 16:14