簡體   English   中英

附加對象值javascript

[英]Append object values javascript

如何附加customerApplicationAddress值no,街道,barangay,直轄市,省並將其放在customerApplication.address中

data(){
    return {
        customerApplicationAddress: {
          no: '',
          street: '',
          barangay: '',
          municipality: '',
          province: '',
        },
        customerApplication:{
          name: null,
          address: null
        }
    }
}

知道我該如何實現嗎? 我嘗試循環並連接它,但是以某種方式我遇到了未定義的錯誤。

考慮將計算的屬性用於customApplication 您的代碼如下所示:

Vue.extend({
  data() {
    return {
      customerApplicationAddress: {
        no: '',
        street: '',
        barangay: '',
        municipality: '',
        province: '',
      },
      customerApplication:{
        name: null,
        address: null
      }
    };
  },

  computed: {
    customerApplication() {

      const addr = this.customerApplicationAddress;

      return {
        // Or consider using string interpolation
        address: addr.no + ' ' + addr.street + ' ' + addr.barangay + ' ' + addr.municipality + ' ' + addr.province
      };
    }
  }
});

這樣,您的customerApplication對象將在每次底層customerApplicationAddress.*值更改時自動更新。

看起來您正在嘗試將一個對象轉換為鍵值對數組,然后轉換為字符串。

您可以使用Object.keys然后使用Array.prototype.map來執行此操作。

 const data = { customerApplicationAddress: { no: '1', street: 'Big Street', barangay: 'some barangay', municipality: 'some municipality', province: 'some province', }, customerApplication:{ name: null, address: null } } data.customerApplication.address = Object.keys(data.customerApplicationAddress).map(k => data.customerApplicationAddress[k]).join(", ") console.log(data) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM