1

I am having trouble sorting out the undefined from the array of objects that was crated from local storage. Lets assume that this array of of objects is localStorage:

var arrObject = [{date: undefined, bus_name: Thomas #1};...] Assume this has 2 dates that had undefined.

I want to be able to filter out the date that has undefined and the bus_name that is within the date so for example, if I used filter for an array of objects before sorting them, then {date: undefined, bus_name: Thomas #1} will not be included in the array that will be sorted.

How would I accomplish this?

Thanks!

UPDATE: 3/5/20

How would I accomplish this if I have more than 2 columns, lets say I have at least 5, I want to filter and sort date as well as only output date and bus_name

var arrObject = [{date: undefined, bus_name: Thomas #1, bus_driver: Thomas, time_start: 9AM, time_end: 5PM};...]

Output: {date: ..., bus_name:...}; {...}

4
  • 5
    arrObject.filter(e => e.date) Commented Mar 3, 2020 at 19:23
  • @danh So I made a function that would create points for that array, how would I implement that onto there. function getPointsTable(data, points) { for (let i in data) { let point = []; let cols = Object.keys(data[0]); for (let j of cols) { point.push(j === 'date' ? Number(new Date(data[i][j])) : data[i][j]); } points.push(point) } return points } Commented Mar 3, 2020 at 19:27
  • @MarkAAvila Just to make sure I understand the question, you need to filter out all objects where date: undefined so that the array will only contain objects that have a date? Commented Mar 3, 2020 at 19:33
  • @JoshuaKleveter yes, dont forget that it will filter out the date that's undefined and the bus_name that contains it for example '{date: undefined, bus_name: Thomas #1}' should not be in the sorted array Commented Mar 3, 2020 at 19:36

2 Answers 2

2

i think you should provide filter with condation field (one is date, another is bus_name )

anyway i want clear it out that :--

 i) arrObject.filter(e => e.date)  // with this all {date: undefined } contain object will be filter and get data which have actual date value 

 ii) arrObject.filter(e => e.date === undefined) // give filter result with all date undefined  
iii) arrObject.filter(e => e.date === undefined && e.bus_name ) or arrObject.filter(e => e.date && e.bus_name)

which one of the result you are expecting

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

2 Comments

The goal is for filter to filter out the date thats undefined or empty along with bus_name, since they are within the same array so Im assuming that the third option is the correct one.
i think your sort portion you achieve by this arrObject1.filter((d) =>{ return d.date}).sort((a,b)=>new Date(b.date) - new Date(a.date))
1

You can use this method:

const arrObject = [
{date: undefined, bus_name: 'Thomas #1'},
{date: '2012-02-11', bus_name: 'Thomas #2'},
{date: '2012-02-02', bus_name: 'Thomas #3'},
{date: '2012-02-04', bus_name: 'Thomas #4'},
{date: undefined, bus_name: 'Thomas #5'},
{date: '2012-02-03', bus_name: 'Thomas #6'},
{date: '2012-02-03', bus_name: 'Thomas #7'},
]
function formatTheDate (str){
  //your format here
  let FormatedDate = "new Date format" + str 
  return FormatedDate
}

let newArray = arrObject.filter( obj => {
  obj.formattedDate = formatTheDate(obj.date)
  //behave same as obj.date != undefined
  return obj.date
}).sort((a,b)=>{
  return Date.parse(a.date) - Date.parse(b.date)
})

console.log(newArray);


EDIT: updated the answer to return sorted result based on the date;

10 Comments

This worked pretty well! Thank You. What if I wanted to sort the array by dates instead dates value being foo and bar its "2012-03-03" and "2013-02-02" instead.
@MarkAAvila Descending or Ascending ?
From early to latest (2012-2020) for example
@MarkAAvila the date format matters, being timespan, being Date(), or string. what kind of date is yours?
the dates are with " " so strings
|

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.