两个数组中用第二个数组的Value对比换第一个数组中的Key

2022-08-04 10:58:26 浏览数 (1)

代码语言:javascript复制
<script type="text/javascript">
      var data = [
        {
          efge: "333",
          test1: "111",
          test1243: "222",
          createBy: "admin",
          createTime: "2022-07-28 10:50:52",
          id: "1552486719464116226",
          planCode: "avc",
          _XID: "row_103",
        },
        {
          efge: "123",
          test1: "456",
          test1243: "789",
          createBy: "admin",
          createTime: "2022-07-28 10:50:52",
          id: "1552486719464116230",
          planCode: "avc",
          _XID: "row_103",
        },
      ];
      var data1 = [
        {
          indexDesc: "efge",
          indexName: "decimal_field1",
        },
        {
          indexDesc: "test1",
          indexName: "implement_rule",
        },
        {
          indexDesc: "test1243",
          indexName: "assessment_item",
        },
      ];
      function valueChangeKey(data, data1) {
        var keys = [];
        var values = [];
        for (var i = 0; i < data1.length; i  ) {
          keys.push(data1[i].indexDesc);
          values.push(data1[i].indexName);
        }
        for (var j = 0; j < data.length; j  ) {
          for (var i = 0; i < keys.length; i  ) {
            if (data[j][keys[i]]) {
              data[j][values[i]] = data[j][keys[i]];
              delete data[j][keys[i]];
            }
          }
        }
        console.log(data);
        return data;
      }
      valueChangeKey(data, data1);
    </script>
key

0 人点赞