[英]How to use Rest pattern to destruct only desired part of an object?
我有以下 object:
openingHours:{
thu: {
open: 12,
close : 22
},
fri: {
open:11,
close:23
},
sat: {
open: 0,
close:24
}
}
我想通过 rest 模式得到一个 object 只有thu和sat例如:
{
thu: {
open: 12,
close : 22
},
sat: {
open: 0,
close: 24
}
}
我知道我可以通过以下方式做到这一点: const{fri,...otherDays} = openingHours
但是不需要fri变量! 有没有办法以我不需要创建冗余变量的方式破坏它
您可以在不获取 object 的所有属性的情况下进行破坏。只需获取 thu 和 sat,然后创建一个新的 object,如下所示:
const openingHours = { thu: { open: 12, close: 22 }, fri: { open:11, close:23 }, sat: { open: 0, close:24 } }; const { thu, sat } = openingHours; const newObject = { thu, sat }; console.log(newObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.