JavaScript Array: Order Nested Object by Key [duplicate] - javascript

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
How to sort strings in JavaScript
(16 answers)
Sort Array by ISO 8601 date
(8 answers)
Closed last year.
I have the following nested object. I would like to iterate through every item (latest.sentTime) and then sort the object itself by "sentTime". So that the most recent message is on top. How can I achieve this?
In the following example, "0" and "1" need to be swapped basically, since "1" has a more recent "sentTime".

Using Array#map, iterate over the array to get latest.sentTime
Using Array#sort and Date, sort the items
const arr = [
{ latest: { sentTime: '2022-02-05T19:15:32.000Z' } },
{ latest: { sentTime: '2022-02-06T22:12:00.000Z' } }
];
const sentTimes = arr
.map(({ latest }) => latest.sentTime)
.sort((a, b) => new Date(b) - new Date(a));
console.log(sentTimes);

Related

SUM AND GROUPING JSON DATA USING JAVASCRIPT [duplicate]

This question already has answers here:
How to group by and sum an array of objects? [duplicate]
(2 answers)
Sum JavaScript object propertyA values with the same object propertyB in an array of objects
(12 answers)
Group by, and sum, and generate an object for each array in JavaScript
(4 answers)
ES6 Implementation of Group By and SUM
(4 answers)
Javascript array of objects group and sum items
(4 answers)
Closed last year.
Sorry if this has been asked before, but I couldn't find a good example of what I'm trying to accomplish. Maybe I'm just not searching for the right thing. Please correct me if there's an explanation of this somewhere.
so let's says I have a data like this :
data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
]
what i wanted to is an output like this :
result = [
{"location":"New York","transaction":12000},
{"location":"Tokyo","transaction":9000},
{"location":"Amsterdam","transaction":6000}
{"location":"Manchester","transaction":3000}
]
so what i wanted to do is grouping the data based on location and sum the transaction where the location is same and push the data to another array. i don't know where to start, need some help to solve this or any suggestion to solve this using Javascript. thank you
Working Demo :
// Input array
const data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
];
// result array
const resultArr = [];
// grouping by location and resulting with an object using Array.reduce() method
const groupByLocation = data.reduce((group, item) => {
const { location } = item;
group[location] = group[location] ?? [];
group[location].push(item.transaction);
return group;
}, {});
// Finally calculating the sum based on the location array we have.
Object.keys(groupByLocation).forEach((item) => {
groupByLocation[item] = groupByLocation[item].reduce((a, b) => a + b);
resultArr.push({
'location': item,
'transaction': groupByLocation[item]
})
})
console.log(resultArr)

How to merge two arrays to make an object [duplicate]

This question already has answers here:
Create an object from an array of keys and an array of values
(9 answers)
Closed 1 year ago.
consider i have an array say
let arr1=["john","Bruce","Clent"];
and
let arr2=[55,33,22];
Then how can i make an object out of this in javascript
object should look like:{"john":55,"Bruce":33,"Clent":22};
it should take arr1 as object's keys and arr2 as object'values
You can just loop the array, but use the index to match the key and value.
const arr1=["john","Bruce","Clent"];
const arr2=[55,33,22];
const obj = {};
arr1.forEach((val, i) => {
obj[val] = arr2[i];
});

Convert array to object with key and value in javascript [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 5 years ago.
How covert or add array elements into object.
Now
arr = [
["1/1/2017",113],
["3/11/2017",58],
["3/12/2017",70],
["3/13/2017",76]
]
Should be
arr = [
{date:"1/1/2017", count:113},
{date:"3/11/2017", count:58},
{date:"3/12/2017", count:70},
{date:"3/13/2017", count:76}
]
arr.map(function(arr1) {
return {
date : arr1[0],
count : arr1[1]
}
})

Sort array of array on string value in Javascript [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 6 years ago.
I want to sort the array of array on length of string, as follows
var arr = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
var sortedArr = sortOnLengthOfString(arr);
>> sortedArr = [
[1951, "Mayfer"],
[1101, "Cetaphil"],
[1007, "Ferri Injection"],
[1785, "Actinac Capsules"]
]
Is there a solution with lodash preferably? Or plain Javascript?
I haven't found duplicate question yet. Can someone find it? Please note, the sorting I am asking for, is for array of arrays and not array of objects.
You can use Array#sort at this context,
var obj = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
obj.sort(function(a,b){
return a[1].length - b[1].length
});
console.log(obj);
//[[1951, "Mayfer"],[1101, "Cetaphil"],[1007, "Ferri Injection"],[1785, "Actinac Capsules"]]

Sort array by date - javascript [duplicate]

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Sort array of objects by single key with date value
(19 answers)
Closed 8 years ago.
I want to sort an array of objects by date, but the problem is their date is in this format - 2014-07-17T13:49:12.767Z.
Here an example of one object in this array
{
id: 578,
creationDate: "2014-07-16T20:56:04.710Z",
creationUser: "FCOUT",
modificationDate: "2014-07-17T13:49:12.767Z",
modificationUser: "FCOUT",
name: "Regra Filipe",
description: "Teste",
type: "Message",
regulation: null,
structure: 1,
deleted: false,
}
I have to sort them by modification date or creation date!
Just write a sort function as given here. In your case the compare function will just be comparing two strings , which in the date format you have mentioned should work out of the box.
ie you can compare the two dates as strings.
Mozilla sorting documentation
use the compare function
function(a, b) {
if (a.creationDate < b.creationDate) { return -1; }
if (a.creationDate > b.creationDate) { return 1; }
return 0;
}
Your date format allows the creationDate strings to just be compared lexicographically

Categories

Resources