普通对象一级、两级赋值

  • 代码如下:
{
    const setKeyValue =(key, val, obj)=> {
        if (key.includes('.')) {
          const firstKey = key.split('.')[0];
          const lastKey = key.split('.')[1];
          obj[firstKey][lastKey] = val;
        } else {
          obj[key] = val;
        }
        return obj;
    }
    const obj = {
        children:{
            attr:{
                name: 'hei~',
                hobby: 'football'
            }
        }
    };
    setKeyValue("children.attr",{name:"naughtyBoy"},obj);
    console.log(obj); //{}
}

判断是否Mac系统

/** * 是否为mac系统(包含iphone手机) * */ 
var isMac = function() { return /macintosh|mac os x/i.test(navigator.userAgent); 
}();

eagle007
34 声望0 粉丝