更新javascript对象属性? [英] Updating javascript object property?

查看:85
本文介绍了更新javascript对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下:

skillet.person = {
  name: {
    first: '',
    last: ''
  }, 
  age: {
    current: '' 
  },
  birthday: {
    day: '',
    month: '',
    year: ''
  }
}

我想知道如何更新这些值?即我认为以下是正确的

I was wondering how I would update these values ? i.e. I though the following was correct

skillet.person.name.push({ first: 'blah', last: 'ha'});

但这是错误的?我该如何解决这个问题?

but it's wrong ? How can I fix this ?

推荐答案

如果你想将一个对象混合到另一个对象中,你可以使用 jQuery 的深度扩展功能.深"意味着它不会用新对象覆盖name,而是覆盖这样一个对象内部的属性.

If you want to mix an object into another one, you can use jQuery's deep extend function. "Deep" means that it does not overwrite name with the new object, but rather overwrites the properties inside such an object.

$.extend(true, skillet.person, {
  name: {
    first: 'updated'
  },
  birthday: {
    day: 'updated',
    year: 'updated'
  }
});

现在,skillet.person 更新了相应的属性,而其他属性保持不变.

Now, skillet.person has the appropriate properties updated, while the other properties are untouched.

这篇关于更新javascript对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆