如何根据类别对产品进行分类

时间:2019-02-15 13:47:03

标签: javascript arrays ionic-framework

能不能帮我一些代码,我正在构建一个购物清单应用程序。因此,我有一系列产品,在每个产品对象中都有一个类别名称。我想做的是使用ngFor在每个类别下显示产品,例如类别名称下的新鲜食物,所有包含新鲜食物的东西都将被显示出来,依此类推。这是我的数组:

var products = [{
    "id": "219",
    "product_id": "198909",
    "sku": "20148089",
    "prod_name": "Chocolate Mousse 1L",
    "vendor": "Woolworths",
    "price": "45.09",
    "status": "0",
    "category": "Bakery & Desserts"
}, {
    "id": "220",
    "product_id": "199403",
    "sku": "20008307",
    "prod_name": "Medium Carrots 500g",
    "vendor": "Woolworths",
    "price": "10.99",
    "status": "0",
    "category": "Carrots & Beetroot"
}, {
    "id": "221",
    "product_id": "204759",
    "sku": "6009207908908",
    "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g",
    "vendor": "Woolworths",
    "price": "40.78",
    "status": "0",
    "category": "Fresh Food"
}, {
    "id": "222",
    "product_id": "199015",
    "sku": "6009182131643",
    "prod_name": "Bulk White Gouda Cheese 900g",
    "vendor": "Woolworths",
    "price": "126.45",
    "status": "0",
    "category": "Cheese"
}]

3 个答案:

答案 0 :(得分:0)

    var products = [{ "id": "219",
                     "product_id": "198909", 
                     "sku": "20148089",
                     "prod_name": "Chocolate Mousse 1L",
                     "image_url": "https://www.onecart.co.za/wp-content/uploads/2018/11/Chocolate-Mousse-1L-20148089.jpg",
                     "vendor": "Woolworths",
                     "price": "45.09", "status": "0", 
                     "category": "Bakery & Desserts" },
                    { "id": "220", "product_id": "199403", 
                     "sku": "20008307",
                     "prod_name": "Medium Carrots 500g",
                     "image_url": "https://www.onecart.co.za/wp-content/uploads/2018/11/Medium-Carrots-500g-20008307.jpg", 
                     "vendor": "Woolworths",
                     "price": "10.99", "status": "0",
                     "category": "Carrots & Beetroot" },
                    { "id": "221", "product_id": "204759",
                     "sku": "6009207908908",
                     "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g",
                     "image_url": "https://www.onecart.co.za/wp-content/uploads/2018/05/Fresh-Spicy-Lentil-Vegetable-Soup-600g-6009207908908-3.jpg",
                     "vendor": "Woolworths",
                     "price": "40.78", "status": "0",
                     "category": "Fresh Food" },
                    { "id": "222", "product_id": "199015",
                     "sku": "6009182131643", 
                     "prod_name": "Bulk White Gouda Cheese 900g",
                     "image_url": "https://www.onecart.co.za/wp-content/uploads/2018/11/Bulk-White-Gouda-Cheese-900g-6009182131643-1.jpg", 
                     "vendor": "Woolworths", "price": "126.45",
                     "status": "0", "category": "Cheese" }];
    
    function dynamicSort(property) {
        var sortOrder = 1;
    
        if(property[0] === "-") {
            sortOrder = -1;
            property = property.substr(1);
        }
    
        return function (a,b) {
            if(sortOrder == -1){
                return b[property].localeCompare(a[property]);
            }else{
                return a[property].localeCompare(b[property]);
            }        
        }
    }
    
    products.sort(dynamicSort("category"));
    console.log(products.sort(dynamicSort("category")));

答案 1 :(得分:0)

我认为您首先要处理的数据集有误。应该是类别数组,其中每个类别对象都包含属于该类别的项目数组。

但是,如果您不能对数据做任何事情,则需要像这样进行转换(对TypeScript感到抱歉,只是对于没有类型的JS它应该是相同的:)):

// To get categories
let categories: any = {};
products.forEach(product => {

  if (!Object.keys(categories).includes(product.category)) {
    categories[product.category] = {
      items: []
    };
  }
});

// Add items to categories
products.forEach(product => {
  Object.keys(categories).forEach(category => {
    if (product.category === category) {
      categories[category].items.push(product);
    }
  });

});

console.log(categories);

答案 2 :(得分:0)

FILE_UPLOAD_MAX_MEMORY_SIZE = N1 * (1024**2)
DATA_UPLOAD_MAX_MEMORY_SIZE = N2 * (1024**2)