简体   繁体   English

我有一些 class 的实例,我将它们放入一个数组中。 我可以遍历数组并更新所有数组对象的价格吗?

[英]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我还有两个继承自 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?是否可以循环遍历数组,使用Pictures class中的updatePrice方法更新所有对象的价格值?

Yes, you can, just use.map, .forEach or for loop是的,你可以,只需使用 .map、.forEach 或 for 循环


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

But I can recommend to store 'list' inside class.但我可以建议将“列表”存储在 class 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将标记为“价格”的所有 class 值存储到一个数组中以便对它们进行排序? - How do I store all my class values labeled "price" into an array so I can sort them? 我有两个数组。 我想以一种可以从另一个数组中获取最接近的值的方式比较它们 - I have two array. I want to compare both of them in a way that I can get the nearest value from the other array 我试图遍历对象数组并将其填充到表中 - I am trying to loop through an array of objects and populate them to a table 我有数据存储在Perl数组中。 如何使用JSON将它们转换为JavaScript数组? - I have data stored in Perl array. How do I convert them into JavaScript array using JSON? 从ajax json请求中,如何将对象动态添加到数组中,以便我可以遍历它们? - From an ajax json request, how can dynamically add the objects to an array so that I can loop through them? 如何在 axios 阵列中插入多个循环。? [等候接听] - How can i insert multiple loop in an axios array.? [on hold] 如何遍历推入数组的Object实例? - How to loop through instances of an Object that are pushed into an array? 如何遍历div数组以显示()? - How can I loop through an array of divs to show() them? 我有一个对象数组,在所有对象中我都删除了一些数据 - I have an Array of objects, in all objects I have delete some data 我有一个对象数组和一个对象,我想遍历对象,同时将对象值与数组中的值匹配 - I have an array of objects and an object I want to loop through an object while matching object values with the values in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM