Json对象平铺展开key
2018-07-08 本文已影响0人
YLPeach
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));