I'm developing the React Web App.
Users will have their array after entering on a certain page.
After getting an array, I want to resort the array by one of the values of startedDate
Here is my code :
sortedCard.forEach( item => {
if(item.startedDate === null || item.expiredDate === null){
item.startedDate = '9999'
}else if(item.startedDate !== null){
item.startedDate = item.startedDate.split('-')[1]+item.startedDate.split('-')[2]
}
} )
I tried map, don't work! And change the current array to the hard coded one(the current array is fetch from server and passed by Redux), it works!
After getting an array, I want to resort the array by one of the values of startedDate
You can sort an array of objects using the sort()
function:
const objs = [ { id: 1, startedDate: '2019-05-01' }, { id: 2, startedDate: '2019-05-03' }, { id: 3, startedDate: '2019-04-10' }, { id: 4, startedDate: '2019-01-21' }, ] objs.sort((a, b) => (a.startedDate > b.startedDate) ? 1 : ((b.startedDate > a.startedDate) ? -1 : 0)) console.log(objs)
This code loops over each object in the array, grabs the property value startedDate
, then compares it against each other startedDate
for each other object in the array and rearranges them accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.