繁体   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