const array= [
0: {id: 4, enterpriseId: 26, date: '2022-01-21', total: 1000}
1: {id: 5, enterpriseId: 26, date: '2022-01-23', total: 1000}
2: {id: 8, enterpriseId: 26, date: '2022-02-10', total: 100}
3: {id: 9, enterpriseId: 26, date: '2022-01-28', total: 20000}
4: {id: 11, enterpriseId: 26, date: '2022-03-08', total: 5000}
5: {id: 12, enterpriseId: 26, date: '2021-10-27', total: 2000}
6: {id: 15, enterpriseId: 26, date: '2022-01-01', total: 200}
7: {id: 16, enterpriseId: 26, date: '2022-01-20', total: 300}
8: {id: 17, enterpriseId: 26, date: '2022-01-05', total: 500}
9: {id: 20, enterpriseId: 26, date: '2022-01-19', total: 200}
]
-
Answer # 1
-
Answer # 2
You pushed an object into array brackets, that's not correct.
You can sort like this:
const obj= { 0: { id: 4, enterpriseId: 26, date: '2022-01-21', total: 1000 }, 1: { id: 5, enterpriseId: 26, date: '2022-01-23', total: 1000 }, 2: { id: 8, enterpriseId: 26, date: '2022-02-10', total: 100 }, 3: { id: 9, enterpriseId: 26, date: '2022-01-28', total: 20000 }, 4: { id: 11, enterpriseId: 26, date: '2022-03-08', total: 5000 }, 5: { id: 12, enterpriseId: 26, date: '2021-10-27', total: 2000 }, 6: { id: 15, enterpriseId: 26, date: '2022-01-01', total: 200 }, 7: { id: 16, enterpriseId: 26, date: '2022-01-20', total: 300 }, 8: { id: 17, enterpriseId: 26, date: '2022-01-05', total: 500 }, 9: { id: 20, enterpriseId: 26, date: '2022-01-19', total: 200 }, }; let res= Object.values(obj).sort((a, b)=> { return a.date > b.date ? eleven; }); console.log('res', { ...res });
The result will be the following:
res { '0': { id: 12, enterpriseId: 26, date: '2021-10-27', total: 2000 }, '1': { id: 15, enterpriseId: 26, date: '2022-01-01', total: 200 }, '2': { id: 17, enterpriseId: 26, date: '2022-01-05', total: 500 }, '3': { id: 20, enterpriseId: 26, date: '2022-01-19', total: 200 }, '4': { id: 16, enterpriseId: 26, date: '2022-01-20', total: 300 }, '5': { id: 4, enterpriseId: 26, date: '2022-01-21', total: 1000 }, '6': { id: 5, enterpriseId: 26, date: '2022-01-23', total: 1000 }, '7': { id: 9, enterpriseId: 26, date: '2022-01-28', total: 20000 }, '8': { id: 8, enterpriseId: 26, date: '2022-02-10', total: 100 }, '9': { id: 11, enterpriseId: 26, date: '2022-03-08', total: 5000 } }
If the output is an array, replace
{...res}
on theres
.
Get the following look:res[ { id: 12, enterpriseId: 26, date: '2021-10-27', total: 2000 }, { id: 15, enterpriseId: 26, date: '2022-01-01', total: 200 }, { id: 17, enterpriseId: 26, date: '2022-01-05', total: 500 }, { id: 20, enterpriseId: 26, date: '2022-01-19', total: 200 }, { id: 16, enterpriseId: 26, date: '2022-01-20', total: 300 }, { id: 4, enterpriseId: 26, date: '2022-01-21', total: 1000 }, { id: 5, enterpriseId: 26, date: '2022-01-23', total: 1000 }, { id: 9, enterpriseId: 26, date: '2022-01-28', total: 20000 }, { id: 8, enterpriseId: 26, date: '2022-02-10', total: 100 }, { id: 11, enterpriseId: 26, date: '2022-03-08', total: 5000 } ]
-
Answer # 3
First, the array format is unfortunate: the notation for type
0: {...}, 1: {...}, ...
is not inherent in arrays at all: instead, you can simply enumerate objects without specifying the order.Secondly, you forgot to put commas after each object, which is why there is simply no enumeration as such.
Thirdly, to sort by date, you can use the function, kindly taken from this answer. It first converts the dates of all objects into objects of type
Date
and then find their difference. However, it will only swap the values if the first object has a later date than the second, which causes older objects to go to the beginning.const array= [ { id: 4, enterpriseId: 26 date: '2022-01-21', total: 1000 }, { id: 5 enterpriseId: 26 date: '2022-01-23', total: 1000 }, { id: 8 enterpriseId: 26 date: '2022-02-10', total: 100 }, { id: 9 enterpriseId: 26 date: '2022-01-28', total: 20000 }, { id: 11 enterpriseId: 26 date: '2022-03-08', total: 5000 }, { id: 12 enterpriseId: 26 date: '2021-10-27', total: 2000 }, { id: 15 enterpriseId: 26 date: '2022-01-01', total: 200 }, { id: 16 enterpriseId: 26 date: '2022-01-20', total: 300 }, { id: 17 enterpriseId: 26 date: '2022-01-05', total: 500 }, { id: 20 enterpriseId: 26 date: '2022-01-19', total: 200 }, ] array.sort(function(a, b) { return new Date(a.date) -new Date(b.date); }); console.log(array);
-
Answer # 4
First, the array format is unfortunate: the notation for type
0: {...}, 1: {...}, ...
is not inherent in arrays at all: instead, you can simply enumerate objects without specifying the order.Secondly, you forgot to put commas after each object, which is why there is simply no enumeration as such.
Thirdly, to sort by date, you can use the function, kindly taken from this answer. It first converts the dates of all objects into objects of type
Date
and then find their difference. However, it will only swap the values if the first object has a later date than the second, which causes older objects to go to the beginning.const array= [ { id: 4, enterpriseId: 26 date: '2022-01-21', total: 1000 }, { id: 5 enterpriseId: 26 date: '2022-01-23', total: 1000 }, { id: 8 enterpriseId: 26 date: '2022-02-10', total: 100 }, { id: 9 enterpriseId: 26 date: '2022-01-28', total: 20000 }, { id: 11 enterpriseId: 26 date: '2022-03-08', total: 5000 }, { id: 12 enterpriseId: 26 date: '2021-10-27', total: 2000 }, { id: 15 enterpriseId: 26 date: '2022-01-01', total: 200 }, { id: 16 enterpriseId: 26 date: '2022-01-20', total: 300 }, { id: 17 enterpriseId: 26 date: '2022-01-05', total: 500 }, { id: 20 enterpriseId: 26 date: '2022-01-19', total: 200 }, ] array.sort(function(a, b) { return new Date(a.date) -new Date(b.date); }); console.log(array);
- javascript : Why do I get a jsx file instead of js after building a project with webpack using babel
- javascript : render {routes} in react
- javascript : React. Unloading ads for 10 pieces
- javascript : How to restart an existing React app?
- javascript : The simplest code on React.js does not work
- javascript : React. Send a get request to the server when pressing Enter
- javascript : Running webPack 5 in development mode does not see html page
- javascript : Problem with Route
- javascript : JS: wrap links found inside a string in an element
- javascript : How to optimize useSelector redux
You pushed an object into array brackets, that's not correct.
You can sort like this:
The result will be the following:
If the output is an array, replace
{...res}
on theres
.Get the following look: