嗨,我已经到处找了,但无法完成
我想从阵列中删除过时的(直到昨天)对象
我的阵列看起来像这个
[{"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"}]
请引导我
我想从数组中删除过时的(最多
yesterday
)对象
老实说,你应该记住,永远不要改变你的数据
==>
根据您的需要使用它即可
您可以使用.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));
更优雅的方法是使用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>
filter()
方法创建一个新数组,其中所有元素由所提供的功能实现的测试。
您可以使用Lodash Filter&把它组合起来,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)
过滤数组并保留所需的唯一数组。根据需要添加日期比较逻辑。
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);