我有一个数组,其中包含工作日的对象,我希望通过在“open”或"closes“中包含null的元素来过滤这些对象(我不希望它们存在于我的最终数组中)。
let array = [
[
{"weekday":1,"opens":"09:00","closes":"11:00"},
{"weekday":1,"opens":null,"closes":null}
],
[
{"weekday":2,"opens":"09:00","closes":"11:00"},
{"weekday":2,"opens":"12:30","closes":"17:00"},
{"weekday":2,"opens":"18:00","closes":"null"}
], ...
]
我想返回一个新创建的数组,这样我就不会改变原来的数组。
我目前的解决方案看起来就是这样,但感觉很难看
let newArray = [];
array.forEach( (day, index) => {
day = day.filter( timeblock =>
timeblock.opens != null && timeblock.closes != null
);
newArray.push(day);
});
如何更优雅地过滤嵌套数组?(如果需要,请使用jsfiddle:https://jsfiddle.net/2jukvsoy/1/)
发布于 2018-05-28 20:26:10
let newArray = array.map(day =>
day.filter(timeblock =>
timeblock.opens != null && timeblock.closes != null
)
);
https://stackoverflow.com/questions/50566302
复制相似问题