-1

var times = [{
  "Start": "2016-01-26\r\n",
  "End": "2016-01-26\r\n"
}, {
  "Start": "2016-01-21\r\n",
  "End": "2016-01-29\r\n"
}]

I am trying to run a loop in jQuery to get the start and end date but I am unable to fetch the value in a variable. I am very new to JSON so any help will be appreciated.

Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
  • first you want to parse the JSON to an object ... then it's just like any other javascript object - it would be helpful to show what you've tried, so people can gauge what your level of proficiency in javascript is – Jaromanda X Jan 25 '16 at 10:17
  • 4
    There is nothing specific about `jQuery`, you can do this using `for(var i=0;.....)` – Rayon Jan 25 '16 at 10:18
  • @RayonDabre - I'd suggest `[].forEach` personally, but, yes, not a jQuery problem (you could use `$.each` if you REALLY must use jQuery saccharine – Jaromanda X Jan 25 '16 at 10:20
  • @JaromandaX Sir, Any reason to do so ? – Rayon Jan 25 '16 at 10:20
  • @RayonDabre - no reason (forEach vs for), just an opinion - by the way, I wouldn't use the jQuery `$.each` **ever** in this case, I only mentioned it because the question asked for a jQuery solution – Jaromanda X Jan 25 '16 at 10:24
  • @JaromandaX Sir, I was going for `forEach` earlier but [this](https://josephscott.org/archives/2014/10/javascript-performance-for-vs-foreach/) made me think otherwise.. – Rayon Jan 25 '16 at 10:26
  • you should use `$(array).each` its fastest among `for` and `$.each` i found as per Mozzilla `for` loop is better among all. – Parth Trivedi Jan 25 '16 at 10:29
  • @JaromandaX as per mozzilla `for` is faster – Parth Trivedi Jan 25 '16 at 10:34
  • @JaromandaX [Check](http://stackoverflow.com/questions/14808144/each-vs-each-vs-for-loop-in-jquery) – Parth Trivedi Jan 25 '16 at 10:37
  • @RayonDabre - that jsperf looks so very wrong - a simple test using the same code shows `forEach` is almost twice as fast as a for loop (about the same difference as in Mozilla by the way) – Jaromanda X Jan 25 '16 at 10:59
  • @ParthTrivedi `$(array).each(fn)` is 4 times slower than `$.each(array, fn)` – Jaromanda X Jan 25 '16 at 11:00
  • @JaromandaX now i am bit confuse now about that. which one is the best. – Parth Trivedi Jan 25 '16 at 11:03
  • unless you're dealing with an array of MILLIONS of items, it really is a cows opinion :p - I didn't start the "this is the fastest method" argument, but rather than rely on obscure unknown code that clearly does some weird stuff, I benchmarked it myself, and came to the conclusion that `array.forEach(fn)` > `$.each(array, fn)` > `$(array).each(fn)` > `for() {}` when dealing with javascript arrays ... > being FASTER – Jaromanda X Jan 25 '16 at 11:06
  • Never tested it on Mozilla. I found the `for-loop` faster in chrome as well as in Mozilla. Forget _argument_, conclusion is important here.. – Rayon Jan 25 '16 at 11:23

4 Answers4

1

You can use Array.forEach to loop over data and .push to insert into new array.

Also I have added code to remove \r\n. Refer following post for reference.

var data = [{
  "Start": "2016-01-26\r\n",
  "End": "2016-01-26\r\n"
}, {
  "Start": "2016-01-21\r\n",
  "End": "2016-01-29\r\n"
}];

var startDate = [];
var endDate = [];
var trimRegex = /\r?\n|\r/g;
data.forEach(function(obj) {
  startDate.push(obj.Start.replace(trimRegex,''));
  endDate.push(obj.End.replace(trimRegex,''));
});

console.log(startDate, endDate);
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

var obj = [{
  "Start": "2016-01-26\r\n",
  "End": "2016-01-26\r\n"
}, {
  "Start": "2016-01-21\r\n",
  "End": "2016-01-29\r\n"
}];

for (var counter = 0; counter < obj.length; counter++) {
  console.log("Start date: " + obj[counter].Start);
  console.log("End date: " + obj[counter].End);
}
Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Hi, I am trying to add the value `var start_date = obj[ counter ].Start; var end_date = obj[ counter ].End; '2016-01-26': { "status": "RESERVE", "reservationId": "24ba2024-f0dd-4c7d-a744-57396d56fd42" },` instead of date but it is not fetching the value. – Abhishek Singh Jan 25 '16 at 12:20
0

So for getting the value of JSON first we see which data structure we are using for json i.e. it is only single object or a array.

if it a array like yours data is an array in that case we can fetch data like

var k=[{"Start":"2016-01-26\r\n","End":"2016-01-26\r\n"},{"Start":"2016-01-21\r\n","End":"2016-01-29\r\n"}];
for(i=0;i<k.length;i++)
{console.log(k[i].Start)}

Will run a loop till array length and try to fetch key. if its only a object then we can do directly like

var k={"Start":"2016-01-26\r\n","End":"2016-01-26\r\n"}
console.log(k.Start)

I hope it will help

Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
0

you can use jquery each

var times = [{
  "Start": "2016-01-26\r\n",
  "End": "2016-01-26\r\n"
}, {
  "Start": "2016-01-21\r\n",
  "End": "2016-01-29\r\n"
}];
$.each(times, function(index, obj) {
  console.log("Start date: " + obj.Start);
  console.log("End date: " + obj.End);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30