Search code examples
javascriptobjectnested-object

store an object in parent object through javascript


I have an signup form asking firstname, lastname, email, password and I have to save them as object where object key should be firstname like below structure.

firstname(i.e charan ) : {
firstname : 'charan',
lastname : 'naidu',
email: [email protected],
password : 12344,
}

Solution

  • Once you have created the Accounts variable, you can then create a new object on it by just giving it a value. Accounts.sai = { ... }

    const Accounts = {
        chinnu: {
            slitID: 3452309,
            fistName: 'chinnu',
            lastName: 'karam',
            email: '[email protected]',
            password: 'abscs*1',
        }
    }
    
    Accounts.sai = {
        slitID: 3423409,
        fistName: 'sai',
        lastName: 'athul',
        email: '[email protected]',
        password: 'silaka*1',
    }
    
    console.log(Accounts);