美文网首页WEB前端HTML/JS/CSS
Json对象平铺展开key

Json对象平铺展开key

作者: YLPeach | 来源:发表于2018-07-08 01:22 被阅读0次

    https://stackblitz.com/edit/json-object-unfold-key-value

    keyValue.js

    const obj = {};
    export function getKey(v, k = '') {
      for (const key in v) {
        let rKey = k + key;
        if (k.indexOf('[') !== -1) {
          rKey += ']'
        }
        if (v[key] instanceof Array) {
          this.getKey(v[key], `${rKey}[`);
        } else {
          obj[rKey] = v[key];
        }
      }
      return obj;
    }
    
    

    test.ts

    const {
      name:'124',
      ang: 1,
      arr: [1, 2, 3, 4, 5, 6],
      arrs: [['a', 'b'], ['c', 'd']]
    }
    console.log(v, getKey(v));
    

    相关文章

      网友评论

        本文标题:Json对象平铺展开key

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