javascript - 如何根据类的属性对数组进行排序?

标签 javascript

如何对“Product”类中创建并记录在“Shop”类中的产品按价格按增长顺序进行排序。该方法的本质在于按价格排序(可以设置顺序)sortByPrice (顺序)

调用示例:

console.log(shop.sortProductsByPrice(Product.SORT_ORDER_ASC));

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    //method for sorting the product at its price
    sortProductsByPrice(dataset, order, prop) {
        let sorted = dataset.sort((a, b) => {
            let innerA;
            for (let subObjectName in a) {
                let inner = a[subObjectName];
                innerA = inner;
                break;
            }
            let innerB;
            for (let subObjectName in b) {
                let inner = b[subObjectName];
                innerB = inner;
                break;
            }
            return order(innerA[prop], innerB[prop])
        });
        return sorted;
    }
}

const shop = new Shop();
// create products
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("product 2", 1, 700));
shop.addProduct(new Product("product 3", 2, 800));
shop.addProduct(new Product("product 4", 3, 1000));
//sort by the specified key "price"
let res = shop.sortProductsByPrice(shop.products,
    (a, b) => a - b, "price");
console.log(res);

最佳答案

我在迭代中删除了 for...。我不认为它正在做你想要做的事情。

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    //method for sorting the product at its price
    sortProductsByPrice(dataset, order, prop) {
        let sorted = dataset.sort((a, b) => {
            
            return order(a[prop], b[prop])
        });
        return sorted;
    }
}

const shop = new Shop();
// create products
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("product 2", 1, 700));
shop.addProduct(new Product("product 3", 2, 800));
shop.addProduct(new Product("product 4", 3, 1000));
//sort by the specified key "price"
let res = shop.sortProductsByPrice(shop.products,
    (a, b) => a - b, "price");
console.log(res);

关于javascript - 如何根据类的属性对数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50725045/

相关文章:

javascript - 从数组创建未知深度对象的通用解决方案

javascript - 即使值是数字,也会收到 "InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lng: not a number"错误

javascript - 将 <options> 存储在变量中并将它们放入任何 <select> 中

javascript - 如何使用 Jquery 选择第一个 td 元素及其文本

javascript - Angularjs 问题 $http.get 未加载图像

javascript - 哪个是最好的生日日期选择器?

javascript - 将类暴露给全局, typescript

javascript - 嵌套数组问题

javascript - 在每页上插入折叠标记(wkhtmltopdf)

javascript - 我如何使用 Foundation 6 的 Javascript?