0

I am getting the response from ajax as json:

{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}

I need to separate these values in javascript like 2017-05-11, 2017-05-18, etc.

I tried split function in javascript but it failed. How do I achieve it?

I need the dates separately in a loop.

Inacio Schweller
  • 1,986
  • 12
  • 22
Nakshatra
  • 45
  • 1
  • 11

5 Answers5

2

Parse the JSON string using JSON.parse method and get the property which holds the array.

var dates = JSON.parse(respose_data).checkin_date
// or
var dates = JSON.parse(respose_data)['checkin_date']

If it's an object then directly access the property.

var dates = respose_data.checkin_date
// or
var dates = respose_data['checkin_date']

UPDATE : And now iterate over the array using Array#forEach or a simple for loop.

dates.forEach(function(v){
   console.log(v);
})

// or
for(var i = 0;dates.length < i; i++){
   console.log(dates[i]);
})
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

You just use method parse of JSON. After that, you receive array value for property "checkin_date". Just use it as normal array. See code below:

var json = '{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}';
var result = JSON.parse(json);
var checkin_date = result['checkin_date'];
for(var i=0; i<checkin_date.length; i++){
  console.log(checkin_date[i]);
}
Freelancer
  • 837
  • 6
  • 21
0

You can do a simple solution using arrow function:

const json = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}

json.checkin_date.map((date) => date)

If you need it on pre-ES6 JavaScript, just do it like:

var json = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}

json.checkin_date.map(function(date) { return date })

Both ways you will have returned all the dates from the loop.

Inacio Schweller
  • 1,986
  • 12
  • 22
0

You could:

  • Parse content of your request to a JavaScript object using JSON.parse().
  • Take property checkin_date which contains an Array with the list of dates.
  • Loop the Array and do whatever you need which each date.

let respose_data = '{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}';
let dates = JSON.parse(respose_data).checkin_date;
dates.forEach(date=> console.log(date))

More info on JSON.parse()

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • tried but SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data error showing – Nakshatra May 31 '17 at 05:40
  • double check that your JSON is correct and with no sytax error. You can paste your JSON result here http://jsoneditoronline.org/ and see if it validates. – GibboK May 31 '17 at 07:09
0

If you need to loop through each value as a date, you first need to convert the values. Take a look:

var dates = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]};

dates
  .checkin_date
  .map(date => new Date(date)) // here you convert the string dates to values of type Date
  .forEach(date => console.log(date)) // here you loop through every Date and do whatever you want, in this case I just logged the output.
Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35