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.

7
  • split into separate array ???Why?? What is the expected output Commented May 31, 2017 at 4:51
  • 1
    Wait, so you're trying to take an array inside an object, and turn it into another array? Can't you just access the array inside the object? Commented May 31, 2017 at 4:52
  • no i need that dates separately in a loop Commented May 31, 2017 at 4:56
  • Do you wanto loop over the array Commented May 31, 2017 at 5:00
  • yes need to display all dates in a loop Commented May 31, 2017 at 5:10

5 Answers 5

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]);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Not what the OP needs Pranav
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]);
}

1 Comment

Did you "Run code snippet" above? It works normally.
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.

Comments

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()

2 Comments

tried but SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data error showing
double check that your JSON is correct and with no sytax error. You can paste your JSON result here jsoneditoronline.org and see if it validates.
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.

Comments

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.