簡體   English   中英

離子3-以這種(\\“ 9876543210 \\”,\\“ 9876543211 \\”)格式獲取數據

[英]Ionic 3 - get data in a variable with this (\“9876543210\”, \“9876543211\”) format

我想使用以下格式檢索所有用戶的聯系電話:\\“ 9876543210 \\”,\\“ 9876543211 \\”,\\“ 9876543211 \\”,...

我已經從數據庫中加載了所有聯系電話,這是我的代碼:

loadContacts()
{
    let contacts: any = [];
    firebase.database().ref('users').orderByKey().once('value', (items: any) => {

        //console.log(items);
        items.forEach((item) => {

            if(item.val().contact_no != '0')
            {
                contacts.push({
                    contactNo: item.val().contact_no
                });
            }
            this.contactList = contacts;
           console.log("Contacts: ",this.contactList);
       });
   },
   (error) => {
    console.log("Error: ", error);
   });
}

loadContacts()檢索以下格式的聯系人:

加載的聯系人格式

我希望所有聯系電話都采用以下格式:\\“ 9876543210 \\”,\\“ 9876543211 \\”,\\“ 9876543211 \\”,...,並將其存儲在變量中。 提前致謝。

您可以將結果轉換為字符串

    if(item.val().contact_no != '0')
        {
            let editedContact:string = '\"'+item.val().contact_no+'\"';
            contacts.push({
                contactNo: editedContact
            });
        }

console.log將跳過\\ ,但是您可以在模板中獲取它

<pre>{{contactList|json}}</pre>
loadContacts()
{
    let contacts: any = [];
    firebase.database().ref('users').orderByKey().once('value', (items: any) => {
    //console.log(items);
    items.forEach((item) => {

      if(item.val().contact_no != '0')
      {
        contacts.push(item.val().contact_no);
      }
      this.contactList = contacts;
      //console.log("Contacts: ",this.contactList);
    });

    var length = this.contactList.length;
      console.log("Length: ",length);
      if(length > 0)
      {
        for(var i = 0; i <= length; i++)
        {
          if(i > 0)
          {
            this.contacts += '\\\"' + this.contactList[i] + '\\\",' ;
          }
        }
        var contact = this.contacts;
        this.contacts = contact.slice(9,-16);
        console.log("Formatted Contacts: ",this.contacts);
      }
  },
  (error) => {
    console.log("Error: ", error);
  });
}

暫無
暫無

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

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