用for循环解决项目中的问题

该篇博客详细介绍了如何在后端返回的数据基础上,使用JavaScript进行数据转换,以便在前端进行渲染。内容涉及数组遍历、对象属性匹配,以及将数字转换为对应的文字描述。在编辑并提交时,又将这些文字描述重新转换回数字,确保数据一致性。主要涉及的技术包括JSON处理、字符串与数字的互换、条件判断以及异步请求。
摘要由CSDN通过智能技术生成

循环数组 for  foreach.....

循环对象  for  in 

后端返回的数据为num 实际渲染为字符串 需要把num和前端字符串进行匹配,所以利用各种各样的循环  将num  匹配为字符串

    import chinaJSON from '@/assets/ChinaCity.json';

    if (res.success) {
          if (res.data != null && res.data.list.length > 0) {
                //遍历返回数据并更改每一项的内容
              res.data.list.forEach((item) => {

                //匹配省市的循环
                for (let i = 0; i < chinaJSON.length; i++) {
                  if(chinaJSON[i].code==item.provinceNumber){
                    item.provinceNumber=chinaJSON[i].name;
                    var city = chinaJSON[i].cityList;
                    for (let j = 0; j < city.length; j++) {
                     if(city[j].code==item.cityNumber){
                       item.cityNumber=city[j].name
                    } }
                  }
                } 
                let tempColorArr = ["黄色", "蓝色", "绿色"];
                item.carNumColor = tempColorArr[item.carNumColor];
                item.ownerSex = item.ownerSex === 0 ? "男" : "女";
                item.industryMainType =
                  item.industryMainType === 0 ? "其他" : "货运";
                item.industrySecondaryType =
                  item.industrySecondaryType === 0 ? "其他" : "货运";
                if (item.carMainType === 0) {
                  item.carMainType = "其他";
                }
                if (item.carSecondaryType === 0) {
                  item.carSecondaryType = "其他";
                }
                if (item.registerType === 0) {
                  item.registerType = "其他";
                }
                if (item.terminalType === 0) {
                  item.terminalType = "其他";
                }
                item.deviceType = item.deviceType === 0 ? "非视频" : "视频";
              });

              that.tableData = res.data.list;
              that.totalCount = res.data.totalSize;
              that.totalPage = res.data.totalPages;

上面是获取到的数据进行渲染  之后进行编辑 并提交时要将文字再次切换为数字进行提交

 handleEditSubmit() {
      var that = this;
      console.log("handleEditSubmit: " + JSON.stringify(that.editFormData));
      console.log(that.editFormData);
      console.log(Object.keys(that.editFormData));
        for (let i = 0; i < chinaJSON.length; i++) {
          if (chinaJSON[i].name == that.editFormData.provinceNumber) {
            that.editFormData.provinceNumber = parseInt(chinaJSON[i].code);
            var city = chinaJSON[i].cityList;
            for (let j = 0; j < city.length; j++) {
              if (city[j].name == that.editFormData.cityNumber) {
                that.editFormData.cityNumber = parseInt(city[j].code);
              }
            }
          }
        }
        let tempColorArr = ["黄色", "蓝色", "绿色"];
        that.editFormData.carNumColor =tempColorArr.map(item => item).indexOf(that.editFormData.carNumColor);
        that.editFormData.ownerSex = that.editFormData.ownerSex === "男" ? 0 : 1;
        that.editFormData.industryMainType = that.editFormData.industryMainType === "其他" ? 0 : 1;
        that.editFormData.industrySecondaryType =
          that.editFormData.industrySecondaryType === "其他" ? 0 : 1;
        if (that.editFormData.carMainType === "其他") {
          that.editFormData.carMainType = 0;
        }
        if (that.editFormData.carSecondaryType === "其他") {
          that.editFormData.carSecondaryType = 0;
        }
        if (that.editFormData.registerType === "其他") {
          that.editFormData.registerType = 0;
        }
        if (that.editFormData.terminalType === "其他") {
          that.editFormData.terminalType = 0;
        }
        that.editFormData.deviceType = that.editFormData.deviceType === "非视频" ? 0 : 1;

      request({
        url: "/api-car/car/update",
        method: "put",
        data: that.editFormData,
      })
        .then((res) => {
          console.log("/api-car/car/update");
          console.log(res);
          if (res.success) {
            this.editDialogVisible.show = false;
            if (that.inputKey.length > 0) {
              that.handleClickSearch();
            } else {
              that.handleRequestData();
            }
            that.$message({
              message: "修改成功",
              type: "success",
            });
          } else {
            // this.$message({
            //   message: "获取失败",
            //   type: "warning",
            // });
          }
        })
        .catch((err) => {
          // this.$message({
          //   message: "网络错误",
          //   type: "warning",
          // });
          console.log(err);
        });
    },

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值