简体   繁体   中英

How to use Rest pattern to destruct only desired part of an object?

I have the following object:

openingHours:{
    thu: {
        open: 12,
        close : 22
         },
    fri: {
        open:11,
        close:23
         },
    sat: {
        open: 0,
        close:24
         }
    }

I want by rest pattern to get an object only with thu and sat eg:

{
    thu: {
        open: 12,
        close : 22
         },
    sat: {
        open: 0,
        close: 24
         }
}

I know that i can do it in the follwoing way: const{fri,...otherDays} = openingHours

But in don't need the fri variable! Is there a way to destruct it the way i won't need to create a redundant variable fri

You can destruct without taking all the attributes of the object. Just take thu and sat and then create a new object, like this:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM