There is an array of objects with the fields name, phone. I want to convert the values ​​in the phone field through the loop into a string. Only numbers are contained there. Tried both String () and .toString (). and + "".

for (index in contactList){ contactList[index].phone = String(contactList[index].phone); console.log(typeof contactList[index].phone); } console.log(contactList); 

In the loop it displays that the format is String. But when I display the entire array, the phone is displayed in a number. https://gyazo.com/e5a4d1c84e820aa9c167aff0e6f81aa5

1 answer 1

Your contactList changes somewhere in the not shown code (perhaps asynchronously), and in the console you see the updated objects.

 var contactList = [ { name: "John", phone: 1234567890, contactSequence: 1 }, { name: "Test", phone: 4444444444, contactSequence: 2 } ]; console.log(contactList); for (var index in contactList){ contactList[index].phone = String(contactList[index].phone); console.log(typeof contactList[index].phone); } console.log(contactList); 

  • Thank you, it was necessary to immediately check separately. I will dig ... - jax_fd