【问题标题】:How to add another variable into an existing object?如何将另一个变量添加到现有对象中?
【发布时间】:2023-03-02 23:07:01
【问题描述】:

我有一个要通过多个页面传递的对象,我想在其中添加另一个变量,但不知道该怎么做。

我曾尝试将对象等同于我要添加的变量,但这只会覆盖对象。

对象

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
      } 

我要添加的变量

var M = document.getElementById('Mobile').textContent;

【问题讨论】:

  • this.appDetails.M = document.getElementById('Mobile').textContent;
  • 如果我想在对象中插入一个对象而不是一个变量,这是否会以同样的方式工作?

标签: typescript ionic3


【解决方案1】:
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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2019-01-18
    • 2021-07-14
    • 1970-01-01
    • 2011-01-25
    相关资源
    最近更新 更多