我想构建某些UI,它涉及到数组对象的操作,我知道伪代码,但我不知道如何产生输出。以下是我的个案陈述。需要指导。
如果外部日期介于数据的date_from和date_to
之间[{
date:'2017-05-11',
data:[{id: 2, name: 'abc', date_from:'2017-05-03', date_to:'2017-05-05'}]
}]
将类型设置为mid
[{
date:'2017-05-11',
data:[{id: 2, name: 'abc', date_from:'2017-05-03', date_to:'2017-05-05', type:'mid'}]
}]
如果外部日期等于date_from
[{
date:'2017-05-11',
data:[{id: 2, name: 'abc', date_from:'2017-05-11', date_to:'2017-05-15'}]
}]
将类型设置为is_start_left
如果外部日期等于date_to
[{
date:'2017-05-11',
data:[{id: 2, name: 'abc', date_from:'2017-05-01', date_to:'2017-05-11'}]
}]
将类型设置为is_end_right
答案 0 :(得分:1)
您可以使用嵌套conditional (ternary) Operator ?:
并检查date_from
或date_to
或仅返回'mid'
。
function setType(array) {
array.forEach(a => a.data.forEach(o => o.type = a.date === o.date_from ?
'is_start_left' :
a.date === o.date_to ?
'is_end_right' :
'mid'
));
return array;
}
console.log(setType([{ date: '2017-05-11', data: [{ id: 2, name: 'abc', date_from: '2017-05-03', date_to: '2017-05-05' }] }]));
console.log(setType([{ date: '2017-05-11', data: [{ id: 2, name: 'abc', date_from: '2017-05-11', date_to: '2017-05-15' }] }]));
console.log(setType([{ date: '2017-05-11', data: [{ id: 2, name: 'abc', date_from: '2017-05-01', date_to: '2017-05-11' }] }]));

.as-console-wrapper { max-height: 100% !important; top: 0; }