美文网首页
for in 更换key值

for in 更换key值

作者: 可乐不可乐_6e02 | 来源:发表于2020-11-22 16:12 被阅读0次
            const arrayStr = [{code: '1', name: 'jack'}];
            let keyMap = {
              "code": "value",
              "name": "label"
            }
            for (let i = 0; i < arrayStr.length; i++) {
              const obj = arrayStr[i];
              for (const key in obj) {
                if (obj.hasOwnProperty(key)) {
                  const newKey = keyMap[key];
                  if (newKey) {
                    obj[newKey] = obj[key];
                    delete obj[key];
                  }
                }
              }
            }
    
    

    相关文章

      网友评论

          本文标题:for in 更换key值

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