Updating javascript object property?

后端 未结 8 1845
盖世英雄少女心
盖世英雄少女心 2021-02-01 03:26

I have a structure like the following:

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


        
相关标签:
8条回答
  • 2021-02-01 03:36

    Using ES7+ syntax and a functional approach:

    const new_obj = { ...obj, name: { first: 'blah', last: 'ha'} }
    
    0 讨论(0)
  • 2021-02-01 03:38

    I think that is simpler

         let skillet = {
            person: {
                name    : {
                    first: '',
                    last : ''
                },
                age     : {
                    current: ''
                },
                birthday: {
                    day  : '',
                    month: '',
                    year : ''
                }
            }
        };
    
        let update = {
            person: {
                name: {
                    first: 'blah',
                    last : 'ha'
                }
            }
        };
    
        let result = Object.assign(skillet.person, update.person);
    
        console.log(result);
    
    0 讨论(0)
  • 2021-02-01 03:44

    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.

    0 讨论(0)
  • 2021-02-01 03:45

    On recent browsers with ECMAScript 2015, you can do:

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

    which will preserve any existing attribute not listed in the right object.

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

    [EDIT] With ES7, you can do even shorter (but is it clearer?...):

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

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

    0 讨论(0)
  • 2021-02-01 03:52
    skillet.person.name.first = "blah"
    skillet.person.name.last = "ha"
    

    or

    skillet.person.name = {first : "blah", last : "ha"}
    
    0 讨论(0)
  • 2021-02-01 03:52

    As @ramon-diogo wrote with ES7+

    I like to update nested values like:

    let user = {
        name: {
            first: 'john',
            last: 'smith'
        },
        age: 18,
        city: 'new york'
    }
    
    const age = 20;
    
    user = {...user, age}
    
    console.log(user.age)
    // output: 20
    
    
    const newData ={
        age: 22,
        city: 'san francisco'
    };
    
    user = {...user,...newData}
    
    console.log(user.name.first)
    // output: john
    console.log(user.age)
    // output: 22
    console.log(user.city)
    // output: 'san francisco'
    
    0 讨论(0)
提交回复
热议问题