简体   繁体   中英

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

I want to retrieve all user's contact numbers in a variable with this format: \\"9876543210\\", \\"9876543211\\", \\"9876543211\\",...

I have loaded all the contact numbers from database, Here is my code:

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() retrieves contacts in this format:

加载的联系人格式

I want all the contact numbers in this format: \\"9876543210\\", \\"9876543211\\", \\"9876543211\\",... and store it in a variable. Thanks in advance.

You can convert the result to string

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

console.log will skip the \\ , but you can get it in the template

<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);
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM