Filtering undefined or empty strings from array of objects in Javascript

不问归期 提交于 2021-01-06 02:46:52

问题


So I am having trouble creating a function that will filter and sort dates and its data to be placed in an array of objects. The issue I am having is a different but similar scenario. The original question was taken from here: Filtering undefined from array of objects in Javascript

Lets say that var arrObject has more than 2 items in an array. See code below.I want to filter any empty string or undefined values and with any items in the array and sort then asc. order by dates. In the new array, I want to output only date and bus_name. Note the names in the array are different, I want to name then in a different name such as this output:

{Date: '2012-02-11', busName: 'Thomas #2'}. ...

How would I accomplish this?

Thank you!

{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',
]

function fancySort(array){
   let arrayCopy = array.slice(0);
  //your sort logic can be replaced here
  arrayCopy.sort();

  return arrayCopy
}

let newArray = arrObject.filter( obj => {
  //behave same as obj.date != undefined
  return obj.date
})

let sortedDates = fancySort(newArray.map(obj => Date.parse(obj.date)));

let sortedObjArray = sortedDates.map( date=>{
  let tempObj;
  newArray.some( na=> {
    tempObj = na;
    return Date.parse(na.date) == date
  })
  return tempObj;
})

console.log(sortedObjArray);```

回答1:


You could filter unwanted parts, map new objects with wanted properties and sort the array by date.

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }


来源:https://stackoverflow.com/questions/60552032/filtering-undefined-or-empty-strings-from-array-of-objects-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!