let a={name:'111',age:222} 如果b是空对象,a中的属性都是空,如果b中的属性是空,a中的属性是空

let a = { name: '111', age: 222 };
let b = {};

// Iterate over the properties of a
for (let prop in a) {
  if (a.hasOwnProperty(prop)) {
    // Assign an empty string to the property in a
    a[prop] = '';
  }
}

console.log(a);
{name: '', age: ''}
let a = { name: '111', age: 222 };
let b = {};

// Iterate over the keys of a
Object.keys(a).forEach((key) => {
  // Assign an empty string to the property in a
  a[key] = '';
});

console.log(a);
{name: '', age: ''}
//用例
let a = { name: '111', age: 222 };
let b = {name:'333'}; // or any object with properties

// Iterate over the properties of a
for (let key in a) {
  if (a.hasOwnProperty(key)) {
    // If the property exists in b, use its value; otherwise, use an empty string
    a[key] = b[key] !== undefined ? b[key] : '';
  }
}

console.log(a);
{name: '333', age: ''}
posted @   崛起崛起  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示