3

Hi i have searched all over the place and cannot able to get this things done

I want to delete outdated(upto yesterday) objects from array

My array looks like this

 [{"apdate": "09-12-2020", "booked": "14:30"}, 
  {"apdate": "11-12-2020", "booked": "19:30"}, 
  {"apdate": "10-12-2020", "booked": "19:00"}, 
  {"apdate": "17-12-2020", "booked": "14:30"}, 
  {"apdate": "17-12-2020", "booked": "15:30"}, 
  {"apdate": "17-12-2020", "booked": "19:00", "phone": "9898989898"}, 
  {"apdate": "10-01-2021", "booked": "16:00"}, 
  {"apdate": "29-01-2021", "booked": "18:30"}, 
  {"apdate": "01-02-2021", "booked": "14:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {"apdate": "01-02-2021", "booked": "10:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {"apdate": "01-02-2021", "booked": "03:30", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}]

Please guide me

1
  • 1
    What did you try so far? You can simply iterate over the array and compare the "apdate" field with a date which is the max value you want to keep (yesterday e.g.) Commented Feb 1, 2021 at 6:20

3 Answers 3

5

I want to delete outdated(up to yesterday) objects from the array

To be honest, you should keep in mind that never mutate your data

==> Just use it based on your needs.

You can use .filter to filter your data based on a given_date.

const data = [
  {apdate: "09-12-2020", booked: "14:30"}, 
  {apdate: "11-12-2020", booked: "19:30"}, 
  {apdate: "10-12-2020", booked: "19:00"}, 
  {apdate: "17-12-2020", booked: "14:30"}, 
  {apdate: "17-12-2020", booked: "15:30"}, 
  {apdate: "17-12-2020", booked: "19:00", phone: "9898989898"}, 
  {apdate: "10-01-2021", booked: "16:00"}, 
  {apdate: "29-01-2021", booked: "18:30"}, 
  {apdate: "01-02-2021", booked: "14:00", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {apdate: "01-02-2021", booked: "10:00", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {apdate: "01-02-2022", booked: "03:30", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}
  ];
  
const convertStringToDate = (strDate) => {
  const values = strDate.split("-");
  return new Date(values[2], values[1] - 1, values[0]);
};

const filterDataByGivenDate = (data, givenDate) =>  data.filter(r => convertStringToDate(r.apdate) >= givenDate);


const today = new Date();
const yesterday = today.setDate(today.getDate() - 1);

console.log(filterDataByGivenDate(data, yesterday));

A more elegant way is using moment.js

const data = [
  {apdate: "09-12-2020", booked: "14:30"}, 
  {apdate: "11-12-2020", booked: "19:30"}, 
  {apdate: "10-12-2020", booked: "19:00"}, 
  {apdate: "17-12-2020", booked: "14:30"}, 
  {apdate: "17-12-2020", booked: "15:30"}, 
  {apdate: "17-12-2020", booked: "19:00", phone: "9898989898"}, 
  {apdate: "10-01-2021", booked: "16:00"}, 
  {apdate: "29-01-2021", booked: "18:30"}, 
  {apdate: "01-02-2021", booked: "14:00", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {apdate: "01-02-2021", booked: "10:00", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {apdate: "01-02-2022", booked: "03:30", name: "Anand", uuid: "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}
  ];
  
const filterDataByGivenDate = (data, givenDate) => data.filter(r => moment(r.apdate, 'DD-MM-YYYY').isSameOrAfter(givenDate));

const yesterday = moment().subtract(1, 'days');
console.log(filterDataByGivenDate(data, yesterday));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>


The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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

4 Comments

Can you please teach me how to delete the object via function
Oh no, you should keep in mind that never mutate your data. Just use it based on your needs.
It is because it is stored in a firestore document, to keep document size thin i asked for this..
So you can update with the newest filtered data, though :)
2

You can use Lodash Filter & combining it will moment.js will do the trick.

const data = [
    {"apdate": "09-12-2020", "booked": "14:30"}, 
    {"apdate": "11-12-2020", "booked": "19:30"}, 
    {"apdate": "10-12-2020", "booked": "19:00"}, 
    {"apdate": "17-12-2020", "booked": "14:30"}, 
    {"apdate": "17-12-2020", "booked": "15:30"}, 
    {"apdate": "17-12-2020", "booked": "19:00", "phone": "9898989898"}, 
    {"apdate": "10-01-2021", "booked": "16:00"}, 
    {"apdate": "29-01-2021", "booked": "18:30"}, 
    {"apdate": "01-02-2021", "booked": "14:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
    {"apdate": "01-02-2021", "booked": "10:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
    {"apdate": "01-02-2021", "booked": "03:30", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}
];

const filteredData = _.filter(data, item => {
    const apdate = moment(item.apdate, 'DD-MM-YYYY');
    const today = moment();
    const difference = apdate.diff(today, 'days');
    
    // You can modify the condition according to your requirements;
    return difference >= 0;
});

console.log(filteredData)

Comments

1

Filter the array and keep the only ones you need. Add your date comparision logic as your need.

let a = [{"apdate": "09-12-2020", "booked": "14:30"}, 
  {"apdate": "11-12-2020", "booked": "19:30"}, 
  {"apdate": "10-12-2020", "booked": "19:00"}, 
  {"apdate": "17-12-2020", "booked": "14:30"}, 
  {"apdate": "17-12-2020", "booked": "15:30"}, 
  {"apdate": "17-12-2020", "booked": "19:00", "phone": "9898989898"}, 
  {"apdate": "10-01-2021", "booked": "16:00"}, 
  {"apdate": "29-01-2021", "booked": "18:30"}, 
  {"apdate": "01-02-2021", "booked": "14:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {"apdate": "01-02-2021", "booked": "10:00", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}, 
  {"apdate": "01-02-2021", "booked": "03:30", "name": "Anand", "uuid": "cPA7ND7U2jdw7qY6nHf4kWuxyx53"}];
  
a = a.filter(obj => {
  // add your date comparison logic here and return true for elements you want to keep
  // in this example it will keep elements with '01-02-2021'
  // you may add here your logic to compare dates
  return obj.apdate === '01-02-2021';
})

// now this way you have cleared elements from a
console.log(a);
  
  

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.