Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Why does my last customer entry override the previous entry instead of adding it to the customer object? JS

Why does my last customer entry override the previous entry instead of adding it to the customer object? JS Any help would be much appreciated.

class Bank {
    constructor() {
        this.customers = {};


    }
}

Bank.prototype.addCustomer = function (customer) {
    this.customers = customer;
    return this;

}
var bank = new Bank();
var newCustomer1 = bank.addCustomer('Curtis');
var newCustomer2 = bank.addCustomer('Alice');

console.log(bank.customers);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First, customers is not an array, you have declared it as a Javascript Object. Second, when you change it to an array, use the push method to add elements to the end of the array.

I've changed your code accordingly.

class Bank {
    constructor() {
        // Change 2
        this.customers = [];


    }
}

// Change 2
Bank.prototype.addCustomer = function (customer) {
    this.customers.push(customer);

}
var bank = new Bank();
bank.addCustomer('Curtis'); 
bank.addCustomer('Alice');
console.log(bank.customers);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!