From the refer link, I was able to get the sorting and column working. Now there are 2 things I need assist on.
First, is how to change the name of the column to a name I choose, (eg, date -> Date, and bus_name -> busName.
Second, is to remove filtered arrays. I used the accepted answer, and it worked but it filtered the empty string at the end of the array. I want to remove any empty strings and or undefined values in the array without affecting the original array I mapped.
This was the accepted answer:
var array = [{ date: " ", bus_name: 'Thomas #1', driver_name: 'Sam', time_start: '9AM', time_end: '5PM' }, { date: '2012-02-11', bus_name: 'Thomas #2', driver_name: 'Samantha', time_start: '8AM', time_end: '4PM' }, { date: '2011-02-02', bus_name: 'Thomas #3', driver_name: 'Peter', time_start: '12PM', time_end: '7PM' }, { date: '2010-06-04', bus_name: 'Thomas #4', driver_name: 'Eddie', time_start: '11AM', time_end: '6PM' }, { date: " ", bus_name: 'Thomas #5', driver_name: 'Raul', time_start: '4AM', time_end: '1PM' }, { date: '2014-04-03', bus_name: 'Thomas #6', driver_name: 'Jessie', time_start: '5AM', time_end: '2PM' }], result = array .filter(o => o.date !== ' ') .map(({ date, bus_name }) => ({ date, bus_name })) .sort((a, b) => a.date.localeCompare(b.date)); console.log(result);
On the filter part, I want to be able to filter and remove the ' ', 0, and/or undefined from the array that is being mapped. How would I do this? I recommend using the accepted answer I used. The array I am using is in localStorage.
Follow criteria from the link below!
Refer to this link: Filtering undefined or empty strings from array of objects in Javascript
With a few adjustments:
o.date
is truthy (not null
, undefined
, false
, 0, ""
...), by adding o.date &&
sort
callback accordingly var array = [{ date: " ", bus_name: 'Thomas #1', driver_name: 'Sam', time_start: '9AM', time_end: '5PM' }, { date: '2012-02-11', bus_name: 'Thomas #2', driver_name: 'Samantha', time_start: '8AM', time_end: '4PM' }, { date: '2011-02-02', bus_name: 'Thomas #3', driver_name: 'Peter', time_start: '12PM', time_end: '7PM' }, { date: '2010-06-04', bus_name: 'Thomas #4', driver_name: 'Eddie', time_start: '11AM', time_end: '6PM' }, { date: " ", bus_name: 'Thomas #5', driver_name: 'Raul', time_start: '4AM', time_end: '1PM' }, { date: '2014-04-03', bus_name: 'Thomas #6', driver_name: 'Jessie', time_start: '5AM', time_end: '2PM' }], result = array .filter(o => o.date && o.date !== ' ') .map(({ date, bus_name }) => ({ arrival: date, busName: bus_name })) .sort((a, b) => a.arrival.localeCompare(b.arrival)); console.log(result);
NB: I used arrival
as name, as using a name that starts with a capital (like Date
) is not very common practice -- initial capitals are more often reserved for names of constructors/classes
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.