javascriptobjectproperties

Updating javascript object property?


I have a structure like the following:

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

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

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

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


Solution

  • 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'
      }
    });
    

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