Sort an arary of objects by date [duplicate] - javascript

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

Related

How to get value from n-dimensional array - (Javascript) [duplicate]

This question already has answers here:
Flattening multidimensional arrays in javascript
(2 answers)
Merge/flatten an array of arrays
(84 answers)
Closed 20 days ago.
I want to get a String value of multi-dimensional array
and push it into a new Array (That'd be an answer!)
For Example :
var dimStr = [ "First-one", ["Double-one", "Double-two", "Double-three",
["Triple-one", "Triple-two"] ], "First-two" ] ;
And the output should be :
output = ["First-one", "Double-one", "Double-two", "Double-three", "Triple-one",
"Triple-two", "First-two" ] ;
Thank you very much in advance for your kindness.
The output should be a new Array that contains every String value from dimStr
Look at the array method .flat().
output = dimStr.flat(3);

How to get the maximum time and minimum time in an array [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Min/Max of dates in an array?
(13 answers)
Closed 2 months ago.
let departDateArr=['2020-10-13 12:00','2020-10-03 13:00','2020-10-13 8:00','2020-10-16 20:00']
let arriveDateArr=['2022-12-20 00:00','2022-12-20 12:20','2022-12-19 12:20','2022-12-08 12:20']
I want to retrieve the minimum time in the departDateArr array
I want to retrieve the maximum time in the arraveDateArr array
How to get the desired time data through time comparison ?
Your times are in a format that can be compared lexicographically, so you can simply use Array#reduce and string comparison to get the maximum or minimum.
let departDateArr=['2020-10-13 12:00','2020-10-03 13:00','2020-10-13 8:00','2020-10-16 20:00'];
let arriveDateArr=['2022-12-20 00:00','2022-12-20 12:20','2022-12-19 12:20','2022-12-08 12:20'];
let minDepart = departDateArr.reduce((a,b)=>b<a?b:a);
let maxArrive = arriveDateArr.reduce((a,b)=>b>a?b:a);
console.log(minDepart);
console.log(maxArrive);

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'}]

convert array of hashes to array of values in javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I am an array of hashes and I want to convert it to array of values something like this
array of hashes [{text: "James"},{text: "developer"}]
convert this to
array of values ["James", "Developer"]
Please help me achieve this in javascript.
Using Object.values, you can get the values of each object.
const input = [{text: "James"},{text: "developer"}];
const output = input.flatMap((item) => Object.values(item));
console.log(output);

Addition of array objects based on date in javascript? [duplicate]

This question already has answers here:
Sum similar keys in an array of objects
(15 answers)
How to merge duplicates in an array of objects and sum a specific property? [duplicate]
(2 answers)
Closed 3 years ago.
I have an object array of the form
var arrayA = [
{value:3000, date:"01/01/2020"},
{value:4000, date:"02/13/2020"},
{value:5500, date:"01/01/2020"},
{value:2300, date:"03/03/2020"},
{value:1200, date:"02/13/2020"},
{value:4000, date:"03/03/2020"}
];
I want to add values that have the same date and come up with a new array as below.
var _new_arrayA = [
{value:8500, date:"01/01/2020"},
{value:5200, date:"02/13/2020"},
{value:6300, date:"03/03/2020"}
];
I do not know where to start, therefore i don't have any implementation code

Categories