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

Viewed 39

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 e.g:

{
    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

1 Answers

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

Related