简体   繁体   English

从数组中删除 object 到给定日期 javascript

[英]Delete a object from array upto given date javascript

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请指导我

I want to delete outdated(up to yesterday ) objects from the array我想从数组中删除过时的(直到yesterday )对象

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 .您可以使用.filter根据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更优雅的方式是使用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. filter()方法创建一个新数组,其中包含通过提供的 function 实现的测试的所有元素。

You can use Lodash Filter & combining it will moment.js will do the trick.您可以使用Lodash 过滤器并将其组合在一起, 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-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)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用给定值搜索和删除的最佳方法,用于从 object 内的数组中删除并返回 object javascript 的新数组? - best way to search and delete with given values, for delete from array inside the object and return new array of object javascript? 从 JavaScript 的数组中删除 object - Delete object from array in JavaScript 从JavaScript中的数组对象中删除值(jquery) - Delete value from array object in javascript (jquery) 从Javascript数组对象中删除记录 - Delete Record From Javascript Array Object 如何通过引用(javascript)从数组中删除对象? - How to delete an object from array by reference (javascript)? 如何从 javascript 中的给定对象数组中获取当前周 - How to get Current week from given array of objects date in javascript 如何从位于Javascript中另一个对象的数组中删除一个对象? - How to delete an object from an array situated in another object in Javascript? javascript基于另一个具有相同对象和值的数组从数组中删除 - javascript delete from array based on another array with same object and values 如何通过索引从对象数组中的对象数组中删除对象javascript - How to delete object from array of objects in array of objects javascript by index 需要帮助,根据给定的对象值构建javascript数组。 - Need help building a javascript array from given object values.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM