当前位置:网站首页>JS to assign values to two objects with the same attributes

JS to assign values to two objects with the same attributes

2022-06-22 07:40:00 A Huang Junhui a

In our development process, it is possible to use two objects with the same attributes , Assign all the attribute values of this object to another object , What should be done to make it easier ?
as follows

    let A = {
    
      name:"",
      age:"",
      sex:"",
      address:"",
      grade:"",
      like:" having dinner , sleep , Doudou "
    };
    let B = {
    
      name:"huangjunhui",
      age:40,
      sex:" male ",
      address:" The earth is Hubei, China ",
      grade:"",
      like:" Look at the beauty and smile OR Smiling at beautiful women ",  // This attribute will put A In the 
      company:" This attribute is not public , Don't put in A in ",
    }

Here is the code

  Object.keys(A).forEach(i=>{
    A[i] = B[i] || A[i]});

// explain obtain A Array of all keys in , Use forEach If B There is this attribute in and the value of the attribute is not empty , Just put B Attribute to A, If B There is no such attribute in the , Just use A Self

原网站

版权声明
本文为[A Huang Junhui a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220535043994.html