0

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, (e.g, 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

3
  • 1
    It seems to be working as expected and removing empty string.What is the problem? Commented Mar 6, 2020 at 14:48
  • Do you want to filter out the empty array entries, or the entries with an empty date property? Commented Mar 6, 2020 at 14:56
  • If you want to actually learn about what you are doing there instead of just copy&pasting, this can get you started: youtube.com/watch?v=rRgD1yVwIvE Commented Mar 6, 2020 at 14:58

1 Answer 1

1

With a few adjustments:

  • Check that o.date is truthy (not null, undefined, false, 0, ""...), by adding o.date &&
  • Use a full object literal (instead of the ES6 shortcut) to defined other names for the properties, and adapt the 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.