美文网首页
合并对象es5、6、7

合并对象es5、6、7

作者: Elvis_645e | 来源:发表于2018-11-24 16:30 被阅读0次

    方法一:循环遍历
    let obj1 = {name:'a',age:18};

    let obj2 = {name:'b',gender:'man'};

    let obj3 = {};

    for(var attr in obj1){

        obj3[attr] = obj1[attr];

    }

    for(var attr in obj2){

        obj3[attr] = obj2[attr];

    }

    console.log(obj3);//{name: "b", age: 18, gender: "man"}

    方法二:assign
    let obj1 = {name:'a',age:18};

    let obj2 = {name:'b',gender:'man'};

    let obj3 = {};

    Object.assign(obj3,obj1,obj2);

    console.log(obj3);//{name: "b", age: 18, gender: "man"}

    方法三:ES7的对象的拓展运算和ES6的解构赋值
    let obj1 = {name:'a',age:18};

    let obj2 = {name:'b',gender:'man'};

    let obj3 = {...obj1,...obj2};

    console.log(obj3);//{name: "b", age: 18, gender: "man"}

    相关文章

      网友评论

          本文标题:合并对象es5、6、7

          本文链接:https://www.haomeiwen.com/subject/doryqqtx.html