对象扁平化
            
              
                
                  本文最后更新于:2023年3月19日 晚上
                
              
            
            
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | function flatObj(obj) {   const res = {};   const flat = (obj, preKey) => {     if (obj === null || obj === undefined) return;     Object.entries(obj).forEach(([key, val]) => {       if (typeof val === "object")         flat(val, `${preKey ? preKey + "." : ""}${key}`);       else {         if (res[key]) res[`${preKey}.${key}`] = val;         else res[key] = val;       }     });   };   flat(obj);   return res; }
  const obj = {   a: "123",   b: "456",   c: {     a: 789,     e: {       a: 000,     },   }, };
  console.log(flatObj(obj)); 
 
  |