JS --使用map集合代替if,else判断

一.对象使用

	let price = "";
	let obj = { a: "转码中", b: "转码失败", c: "AI审核中", d: "排队中" };
	price=obj[val];

	  //if (val == a) {
      //  price = "转码中";
      // } else if (val == b) {
      //   price = "转码失败";
      // }

二.对象配合函数使用

this.getStatus("AI编目失败");
	 getStatus(val) {
      let list = {
        转码失败: () => {
          this.step = 102;
          this.status = -1;
        },
        文件异常: () => {
          this.step = 102;
          this.status = -2;
        },
      };
      list[val]()

	  // if (val == "转码失败") {
      //   this.step = 102;
      //   this.status = -1;
      // } else if (val == "文件异常") {
      //   this.step = 102;
      //   this.status = -2;
      // } 
    }

三.newMap的使用

	     let statusMap = new Map([
        [
          { role: "ascending", status: "createdAt" },
          () => {
            this.sort = { createTime: "asc" };
          },
        ],
        [
          { role: "descending", status: "createdAt" },
          () => {
            this.sort = { createTime: "desc" };
          },
        ],
        [
          { role: "descending", status: "machineSucceedCount" },
          () => {
            this.sort = { machineSucceedCount: "desc" };
          },
        ],
        [
          { role: "ascending", status: "machineSucceedCount" },
          () => {
            this.sort = { machineSucceedCount: "asc" };
          },
        ],
      ]);
      let getStatus = function (role, status) {
        statusMap.forEach((value, key) => {
          if (
            JSON.stringify(key) ==
            JSON.stringify({ role: role, status: status })
          ) {
            value();
          }
        });
      };
     getStatus(this.column.order, this.column.prop);  //传数据
     console.log(this.sort, "************");

	// if (this.column.order === "ascending" && this.column.prop === "createdAt") {
      //   this.sort = { createTime: "asc" };
      // } else if (this.column.order === "descending" && this.column.prop === "createdAt") {
      //   this.sort = { createTime: "desc" };
      // } else if (
      //   this.column.order === "descending" &&
      //   this.column.prop === "machineSucceedCount"
      // ) {
      //   this.sort = { machineSucceedCount: "desc" };
      // } else if (
      //   this.column.order === "ascending" &&
      //   this.column.prop === "machineSucceedCount"
      // ) {
      //   this.sort = { machineSucceedCount: "asc" };
      // }

你可能感兴趣的:(【JS】,【笔记】,javascript,前端,开发语言)