Home > OS >  Use map object instead of params correctly
Use map object instead of params correctly

Time:12-20

I want to use map object instead of calling checkOrder 3 times. How to use via Map instead of all params in this case? :

Admin.ts

class Admin{
  async checkTest(
    local: string,
    suburb: string,
    international: string
  ): Promise<void> {
    await checkOrder(local, "Declined");
    await checkOrder(suburb, "Declined");
    await checkOrder(international, "Declined");
    return;
  }
}

Test.ts

test("Test", async (t) => {
  await Admin.checkTest(
    "Local Place",
    "Suburb Place",
    "International Place"
  );
  );
}

CodePudding user response:

@Thomas I want to use map object instead of calling checkOrder 3 times .

like this?

class Admin {
  async checkTest(map: Map<Place, Status>) {
    for (let [place, status] of map) {
      await checkOrder(place, status);
    }
  }
}

and

test("Test", async (t) => {
  await Admin.checkTest(
    new Map([
      ["Local Place", "Declined"],
      ["Suburb Place", "Declined"],
      ["International Place", "Declined"],
    ])
  );
});

CodePudding user response:

So, is it correct map using in my case ?

class Admin {
  async checkTest(map: Map<string, string>) {
    for (let [place, status] of map) {
      await checkOrder(place, status);
    }
  }
}

and

test("Test", async (t) => {
  await Admin.checkTest(
    new Map([
      ["Local Place", "Declined"],
      ["Suburb Place", "Declined"],
      ["International Place", "Declined"],
    ])
  );
});

  • Related