var timeslots = [
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
},
{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/26"
}],
"time": "10:30 AM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/26"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/26"
}],
"time": "11:30 AM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/23"
}],
"time": "12:30 PM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
},{
"name":"naveen",
"email":"navee@gmail.com",
"date":"2019/12/16"
}],
"time": "02:30 PM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
}],
"time": "03:30 PM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveeni@gmail.com",
"date":"2019/12/16"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
}],
"time": "04:30 PM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/16"
}],
"time": "05:30 PM",
"status": "Booked"
}
]
基于今天的日期,我有09:00 am --09:00 pm之类的时间段,我需要迭代对象列表,并且需要分别在nodejs,javascript中删除以前的日期对象
所以请帮助我
预期产量
var date ="23/12/2019"
var timeslots = [
{
"details": [
{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/26"
}],
"time": "10:30 AM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveeni@gmail.com",
"date":"2019/12/26"
},{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/26"
}],
"time": "11:30 AM",
"status": "Booked"
},
{
"details": [{
"name":"naveen",
"email":"naveen@gmail.com",
"date":"2019/12/23"
}],
"time": "12:30 PM",
"status": "Booked"
},
{
"details": [],
"time": "02:30 PM",
"status": "Booked"
},
{
"details": [],
"time": "03:30 PM",
"status": "Booked"
},
{
"details": [],
"time": "04:30 PM",
"status": "Booked"
},
{
"details": [],
"time": "05:30 PM",
"status": "Booked"
}
]
我该如何迭代
基于今天的日期,我有09:00 am –09:00 pm之类的时间段,我需要迭代对象列表,并且需要分别在nodejs,javascript中删除以前的日期对象
所以请帮助我
timeslots.time.forEach(function(value) {
console.log(value.time);
value.details.forEach(function(model) {
console.log(value.details);
value.date.forEach(function(date) {
console.log(value.date)
})
});
});
for (var i = 0; i < timeslots.length; i++) {
if (timeslots[i].details[i].date < today) {
list.push(timeslots[i].details[i].date)
}
}
slots.reduceRight(function(acc, obj, idx) {
if (list.indexOf(obj.date) > -1) slots.splice(idx, 1);
}, 0);
答案 0 :(得分:0)
我仔细检查了您的代码,发现有几处不正确的地方可以改进。
var date ="23/12/2019" // string needs to be "2019/12/23" to be parsed by 'Date'
for (var i = 0; i < timeslots.length; i++) { // can be replaced with map
if (timeslots[i].details[i].date < today) { // this is comparing strings, not dates
list.push(timeslots[i].details[i].date) // can be replaced by filter on details
}
}
我已经使用所看到的东西转换了您的代码:
const today = "2019/12/23";
const timeslots = [...]
const answer = timeslots.map(timeSlot => {
return {
...timeSlot, // this is called the spread operator
details: timeSlot.details.filter(detail => {
return new Date(detail.date).getTime() >= new Date(today).getTime()
})
}
})
我们map
timeslots
到一个新对象,使用散布运算符复制现有属性。
然后,我们通过将对象的详细信息置于对象声明的下面,使用数组的过滤版本来覆盖其详细信息。
我们正在基于已解析日期字符串的getTime
结果进行过滤。 getTime
返回Date
对象的纪元时间戳。这使我们的比较更加容易,因为它们只是数字。
答案 1 :(得分:0)
如果我的理解正确,您可以这样考虑您的问题:(1)您要删除所有早于特定日期和(2)那么您要删除所有没有任何详细信息的时隙。
考虑学习object-scan。一旦您将数据包起来,它对于数据处理就非常强大。这是解决问题的方法
const objectScan = require('object-scan');
const prune = (date, input) => {
const jsDate = new Date(date);
const logic = {
'[*].details': (value) => value.length === 0,
'[*].details[*].date': (value) => jsDate > new Date(value)
};
return objectScan(Object.keys(logic), {
rtn: 'bool',
filterFn: ({ matchedBy, key, value, parents }) => {
if (matchedBy.some((n) => logic[n](value))) {
parents[1].splice(key[key.length - 2], 1);
return true;
}
return false;
}
})(input);
};
const timeslots = [{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"}],"time":"10:30 AM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"}],"time":"11:30 AM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/23"}],"time":"12:30 PM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"navee@gmail.com","date":"2019/12/16"}],"time":"02:30 PM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"}],"time":"03:30 PM","status":"Booked"},{"details":[{"name":"naveen","email":"naveeni@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"}],"time":"04:30 PM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/16"}],"time":"05:30 PM","status":"Booked"}];
console.log(prune('2019/12/23', timeslots)); // return true iff changes made
// => true
console.log(JSON.stringify(timeslots));
// => [{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"}],"time":"10:30 AM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"},{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/26"}],"time":"11:30 AM","status":"Booked"},{"details":[{"name":"naveen","email":"naveen@gmail.com","date":"2019/12/23"}],"time":"12:30 PM","status":"Booked"}]