Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
I have some instances of a class and I pushed them into an array. Can I loop through the array and update the price of all the array objects?

I have a parent constructor like this:

let list = [];
class Pictures {
    constructor(price, title) {
        this.price = price;
        this.title = title;
        list.push(this)
    }
    updatePrice(price_increase) {
        this.price = this.price * price_increase / 100 + this.price
        return this.price
    }
}

I also have two child classes that inherit from the Pictures(Parent) class

class Photograph extends Pictures {
    constructor(photographer, camera, aperture, contrast, price, title) {
        super(price, title);
        this.price = price;
        this.title = this.title;
        this.photographer = photographer;
        this.camera = camera;
        this.aperture = aperture;
        this.contrast = contrast;
    }
    alterContrast(new_contrast) {
        this.contrast = new_contrast;
    }
    toString() {
        return `Photographer: ${this.photographer}, Camera: ${this.camera},
         Aperture: ${this.aperture}, Contrast: ${this.contrast}`;
    }
}

And:

class Painting extends Pictures {
    constructor(artist, type, owner, title, price) {
        super(price, title);
        this.price = price;
        this.title = title;
        this.artist = artist;
        this.type = type;
        this.owner = owner;
    }
    printProvenance() {

    }
    toString() {
        return `Artist: ${this.artist}, Type: ${this.type}, Owner: ${this.owner}`;
    }
}

And these are the instances of the classes

let photo_one = new Photograph('Kyle', 'Nikon', 32, 21, 30, 'Sunset')

let photo_two = new Photograph("Maya", "Sony XPR", 100, 23, 100.00, "Festival of Color");

I pushed all of the instances to the "list" array in the parent constructor. Is it possible to loop through the array and use the updatePrice method in the Pictures class to update the price value of all objects?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, you can, just use .map , .forEach or for loop


    list.map((item) => {
        item.updatePrice(1000)
    })

But I can recommend to store 'list' inside class.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda