简体   繁体   English

如何将另一个变量添加到现有对象中?

[英]How to add another variable into an existing object?

I have an object that I am passing through multiple pages and I want to add another variable into it and am not sure how to do that.我有一个对象,我正在通过多个页面,我想向其中添加另一个变量,但不知道该怎么做。

I have tried tried just equating the object to the variable I want to add but that just overwrites the object.我曾尝试将对象等同于我想添加的变量,但这只会覆盖对象。

The Object物体

this.appDetails = {
        Maids: Details.Maids,                  
        length: Details.length,                
        cMats: Details.cMats,                   
        specialReq: Details.specialReq,          
        Address: Details.Address,                 
        Date: Details.Date,                      
        Payment: "By Credit/Debit Card",
        UID: this.id
      } 

The variable I want to add我想添加的变量

var M = document.getElementById('Mobile').textContent;
this.appDetails = {
        Maids: Details.Maids,                  
        length: Details.length,                
        cMats: Details.cMats,                   
        specialReq: Details.specialReq,          
        Address: Details.Address,                 
        Date: Details.Date,                      
        Payment: "By Credit/Debit Card",
        UID: this.id
} 

addNewProperty(){
   this.appDetails.M = document.getElementById('Mobile').textContent;
   //or 
   this.appDetails["M"] = document.getElementById('Mobile').textContent;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将元素添加到现有对象 - How to add an element to an existing object 如何使用 typscript 编译器在 typescript 文件中向现有 json object 添加另一个属性? - How do I add another property to an existing json object in a typescript file using typscript compiler? 如何向数组中的现有 object 添加新值 - How to add a new value to existing object in array 如何使用 typescript 添加另一个 object 到 object? - How to add another object to an object using typescript? 如何使用打字稿或角度在现有对象中添加对象和数组 - How to add an object and array in an existing object using typescript or angular 如何以角度将对象的现有属性复制到另一个对象? - How to copy existing properties of an object to another one in angular? 如何在typescript中添加声明到现有对象(键入augmentation) - How to add declare to existing object (typing augmentation) in typescript 如何在现有对象中添加属性以满足打字稿中的接口? - How to add a property to an existing object to satisfy an interface in typescript? 如何使用角度将新对象添加到现有动态响应中 - how to add new object to the existing dynamic response using angular 如何向数组中的现有 object 添加新键和值(角度) - How to add a new key and value to an existing object in an array (Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM