I am experiencing an unusual behavior with Array_Push(). When I add an array to a predefined array, the length of the array unexpectedly increases by two instead of one.
Below is the code snippet:
Example array:
Object {id: 2, firstName: "First name", lastName: "Last name", phones: Array[0], emails: Array[0]…}$$hashKey: "00A"address: "Street 3"city: "Washington "emails: Array[0]firstName: "First name"id: 2lastName: "Last name"notes: "No notes"phones: Array[0]__proto__: Object
JavaScript Code:
var contacts = [
{
id: 0,
firstName: 'John',
lastName: 'Doe',
phones: [{'phone1': '222222222'}],
emails: [{'email1': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="412439202c312d24012c20282d6f222e2c">[email protected]</a>'}],
address: 'Some Streetz 2.',
city: 'Las Vegas',
notes: 'Napomena',
},
{
id: 1,
firstName: 'Mike',
lastName: 'Smith',
phones: [{'phone1':'111111'}],
emails: [{'email1': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b0adb4b8a5b9b095b8b4bcb9fbb6bab8">[email protected]</a>'}],
address: '459 5th Av.',
city: 'New York',
notes: 'Napomena',
}
];
Adding an array to the contacts array:
this.addContact = function (contact) {
alert (contacts.length);//output is 2
contact.id = contacts.length ++;//getting ID od new array
console.log(contact);//
contacts.push(contact);
alert ("++"+contacts.length);//output is 4
console.log(contacts);
};