美文网首页
两个数组中用第二个数组的Value对比换第一个数组中的Key

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

作者: 吕保鑫 | 来源:发表于2022-07-29 14:39 被阅读0次
    valueChangeKey.png
    <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>
    

    相关文章

      网友评论

          本文标题:两个数组中用第二个数组的Value对比换第一个数组中的Key

          本文链接:https://www.haomeiwen.com/subject/hkkqwrtx.html