in a particular scene, such as m = {"a": 1, "b": {"c": 2, "d": [3jue 4]}}
, his hierarchy must be expanded. The result:
m={"a":1,"b.c":2,"b.d":[3,4]}
my code:
var m = {
"a": 1,
"b": {
"c": 2,
"d": [3, 4],
"e": {
"f":5
}
}
}
function fn(obj) {
if (Object.prototype.toString.call(obj) === "[object Object]") {//obj:{}
var newObj = {};
for (var k in obj) {
if(Object.prototype.toString.call(obj[k]) === "[object Object]"){
for(var j in obj[k]){
if(Object.prototype.toString.call(obj[k][j]) === "[object Object]"){
newObj[k+"."+j] = fn(obj[k][j])
}else{
newObj[k+"."+j] = obj[k][j]
}
}
}else{
newObj[k] = obj[k]
}
}
} else {
return obj
}
return newObj
}
console.log(fn(m))
ada.e.f = 5