[英]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\\",... 我想使用以下格式检索所有用户的联系电话:\\“ 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: loadContacts()检索以下格式的联系人:
I want all the contact numbers in this format: \\"9876543210\\", \\"9876543211\\", \\"9876543211\\",... and store it in a variable. 我希望所有联系电话都采用以下格式:\\“ 9876543210 \\”,\\“ 9876543211 \\”,\\“ 9876543211 \\”,...,并将其存储在变量中。 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 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.