skip to Main Content

I have an object named "colors" which have keys like

{
"white":"#FFFFFF",
"black":"#000000",
"red":"FF0000"
}

In some of the conditions i want all colors except the "white" color

3

Answers


  1. const source = {
    "white":"#FFFFFF",
    "black":"#000000",
    "red":"FF0000"
    };
    
    const target = {...source};
    delete target["white"]
    

    Typescript Version:

    const source = {
    "white":"#FFFFFF",
    "black":"#000000",
    "red":"FF0000"
    };
    
    const target: Partial<typeof source> = {...source};
    delete target["white"]
    
    Login or Signup to reply.
  2. delete source["colourname"]

    Login or Signup to reply.
  3. let color = {
    "white":"#FFFFFF",
    "black":"#000000",
    "red":"FF0000"
    }
    
    let filteredColor = Object.keys(color)
        .filter((key) => !key.includes("white"))
        .reduce((obj, key) => {
            return Object.assign(obj, {
              [key]: color[key]
            });
      }, {});
    

    using the filter and reduce you can filter out the result

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search