JS object reduces dimensionality

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  
Mar.02,2021

as long as you look like yourself, you will continue to call until you are different from yourself and terminate

.

your implementation effect can be found in the following

 var m = {
   "a": 1,
   "b": {
     "c": 2,
     "d": [3, 4],
     "e": {
       "f": 5
     }
   }
 }

 function fn(obj, keys, ret) {
   if (typeof obj === 'object') {
     Object.keys(obj).forEach(key => {
      fn(obj[key], [...keys, key], ret)
     })
   } else {
     ret[keys.join('.')] = obj
   }
 }
 var ret = {}
 fn(m, [], ret)
 console.log(ret)

answer for yourself:

function fn(obj, str) {
            if(Object.prototype.toString.call(obj) === '[object Object]'){
                for(var k in obj){
                    fn(obj[k],str+k+'')
                }
            }else{
                res[str.split('').join('.')] = obj
            }
        }
        fn(m,'');
        console.log(res)

there is another question: why add str strings to an array and then push, the result incorrectly?

function fn(obj, str) {
            if(Object.prototype.toString.call(obj) === '[object Object]'){
                for(var k in obj){
                    // fn(obj[k],str+k+'')
                    str.push(k);
                    fn(obj[k],str)
                }
            }else{
                // res[str.split('').join('.')] = obj
                res[str.join('.')] = obj
            }
        }
        // fn(m,'');
        fn(m,[]);
        console.log(res)

before you answer: because push changed the original str.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35ea1-2bfb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35ea1-2bfb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?