how to sort json data by date [duplicate] - javascript

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Closed 8 years ago.
i want to sort my json response by date ,here is the javascript function
function successCallback(responseObj){
$.each(responseObj.allRecom.recom,function(index){
var data=JSON.stringify(responseObj); //contains json response
alert(responseObj.allRecom.recom[index].recomDate);//it contains date value from response.
var date=new Date(responseObj.allRecom.recom[index].recomDate);
date=date.toLocaleString(); //converted to local date string
});
}
now what i want is whatever the response will be come ,it should be sorted by date.thanks

Put every object in an array and then call these function:
dates.sort(function(a,b){return a-b}); //asc
dates.sort(function(a,b){return b-a}); //desc
just change 'dates' to corresponding name of the array

Related

Javascript - Sort does not work properly with string [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
How to sort an object array by date property?
(25 answers)
Closed 7 months ago.
I am new to JS and trying to sort an array based on a key of the object (each object is an element in the array). I am not sure why the following code does not sort the array properly. I have checked the doc carefully and the code looks correct to me. I tried to replace the date value with numerical numbers and it worked. Not sure what is wrong with the date string.
var dic=[{key: '2022-05-13'}, {key: '2022-05-06'}]
dic.sort(function (a, b) {
return (a.key - b.key);
})
console.log(dic)
Output
[{key: '2022-05-13'}, {key: '2022-05-06'}]
I thought the output should be
[ {key: '2022-05-06'}, {key: '2022-05-13'}]

Why am I getting undefined getting data from my json? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
In my angularjs file I stringified my json result
console.log("details... "+JSON.stringify(response));
it is somewhat of this nature
{"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE",
when I try to console for firstname I get a surprising outcome of undefined
console.log("firstname... "+ response.data.data.firstname) ;
please what could be wrong
The response.data.data is an array, that's what the square brackets are []. You must first access the item itself before accessing the firstname attribute on that item.
For example, to get the first item, you would do
console.log(response.data.data[0].firstname)
Instead of:
console.log("firstname... "+ response.data.data.firstname) ;
Do:
console.log("firstname... "+ response.data.data[0].firstname) ;
This happens because firstname is a key to an object which is inside an array named data.
Your final data is an array. You have to specify index on that:
var response = {"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE"}]}};
console.log("firstname... "+ response.data.data[0].firstname)

Sort an arary of objects by date [duplicate]

This question already has answers here:
Sort array of objects by single key with date value
(19 answers)
Closed 8 years ago.
I got an array of objects which I want to sort by date. I've got the date formatted like this with a property - created_at: "2014-09-26 19:27:43".
If any one could point me in the right direction, that would be great.
Use Date objects for comparison and Array.prototype.sort() to sort.
var array = ["2014-09-26 19:27:43","2014-09-26 19:27:42","2014-09-23 19:27:43"];
var sortedArray = array.sort(function(a,b){
return new Date(a) - new Date(b);
});
document.write(sortedArray);

Store array in jQuery cookie [duplicate]

This question already has answers here:
Create array in cookie with javascript
(10 answers)
Closed 9 years ago.
I am storing array in cookie through jQuery cookie plugin but each time I get it from cookie, it returns empty
I define array as:
var myArray = [];
myArray[ 0 ] = "hello";
myArray[ 1 ] = "world";
Set it in cookie
$.cookie('cookie-array', myArray);
But getting this cookie and printing, prints empty string
console.log($.cookie('cookie-array'));
EDIT:
Arrays define in other questions are object arrays no like this array I mentioned here. Also I dont want to user JSON library.
Have a look at:
https://code.google.com/p/cookies/
to store an array
$.cookie('COOKIE_NAME', escape(myArray.join(',')), {expires:1234});
to get it back
cookie=unescape($.cookie('COOKIE_NAME'))
myArray=cookie.split(',')

Accessing a two dimensional JSON array with Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?
I am using the US Census API and end up with a two dimensional json array from a jQuery.get() request. My result (data) looks like this:
[["P0010001","NAME","state","county","tract"], ["2703","Census Tract 4001.01","17","119","400101"], ["5603","Census Tract 4001.02","17","119","400102"], ["4327","Census Tract 4002","17","119","400200"]]
It looks exactly like a two dimensional javascript array, but I cannot access it like one when I try:
var population = data;
alert(population[1][0]);
Is there a way to convert the json array into a javascript array, or to convert it to a string, which could then be put into an array?
Use JSON.parse:
var population = JSON.parse(data);
alert(population[1][0]);
JsFiddle: http://jsfiddle.net/6CGh8/

Categories