Moooonmay 2022-04-27 14:18 采纳率: 100%
浏览 198
已结题

关于Js中简化Json数据赋值另一个Json的问题!(语言-javascript)

我的需求就是想 将 一个json中的部分数据,传给另一个json
数据例如下 想要将 Json_two 中的 other_oneother_two 等部分数据 赋值给 Json_one

  let Json_one = {
    attr_one : '1',
    attr_two : '2',
    attr_three : '3'
  }
  let Json_two = {
    other_one : '1',
    other_two : '2',
    other_three : '3',
    other_four : '4'
  }

我用最原始的方式

  Json_one.other_one = Json_two.other_one
  Json_one.other_two = Json_two.other_two
  Json_one.other_four = Json_two.other_four

我想到的其他方式,已简便

  Object.assign(Json_one, {
    other_one : Json_two.other_one,
    other_two : Json_two.other_two,
    other_four : Json_two.other_four
  })
// 结果 
Json_one = {
    attr_one : '1',
    attr_two : '2',
    attr_three : '3',
    other_one : '1',
    other_two : '2',
    other_four : '4'
  }

我就是想咨询下,有没有更简便的方法,我只用传指定要赋值的 键名就可以完成赋值,例如传 other_oneother_two

Thanks♪(・ω・)ノ

  • 写回答

1条回答 默认 最新

  • 雾里桃花 2022-04-27 14:29
    关注
    
    let Json_one = {
        attr_one : '1',
        attr_two : '2',
        attr_three : '3'
    }
    let Json_two = {
        other_one : '1',
        other_two : '2',
        other_three : '3',
        other_four : '4'
    }
    let {other_one, other_two} = Json_two;
    Json_one = {...Json_one, other_one, other_two};
    console.log(Json_one) //{"attr_one":"1","attr_two":"2","attr_three":"3","other_one":"1","other_two":"2"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月27日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来