需要根据特定ID

时间:2018-05-20 11:00:11

标签: javascript sorting mapping

你好,我在java脚本&有问题排序值和现在按类别得到总和我有hotel_id和category_id。

    let myarray = [
  {
    price: 257,
    category: 1,
    hotel_id: 4
  },
  {
    price: 493,
    category: 1,
    hotel_id: 3
  },
  {
    price: 514,
    category: 1,
    hotel_id: 3
  },
  {
    price: 257,
    category: 1,
    hotel_id: 3
  },
{
    price: 104,
    category: 1,
    hotel_id: 3
  },
  {
    price: 295,
    category: 1,
    hotel_id: 3
  },
  {
    price: 125,
    category: 1,
    hotel_id: 2
  },
  {
    price: 125,
    category: 1,
    hotel_id: 2
  },
  {
    price: 157,
    category: 1,
    hotel_id: 2
  },
  {
    price: 125,
    category: 1,
    hotel_id: 2
  },
{
    price: 125,
    category: 1,
    hotel_id: 1
  },

  {
    price: 43,
    category: 1,
    hotel_id: 1
  },
  {
    price: 43,
    category: 2,
    hotel_id: 1
  },
  {
    price: 43,
    category: 2,
    hotel_id: 1
  }
];

var hotel_to_values = myarray.reduce(function (obj, item) {
    obj[item.hotel_id] = obj[item.hotel_id] || [];
    obj[item.hotel_id].push(item.category);
    return obj;
}, {});

var hotels = Object.keys(hotel_to_values).map(function (key) {
    return {hotel_id: key, category: hotel_to_values[key]};
});

我需要按照这样的方式进行排序或分组

  • 酒店1
    • 类别1
      • 价格20
    • 类别2
      • 价格20,价格30

现在我的结果是

[
    {
        "hotel_id": "1",
        "category": [
            1,
            1,
            2,
            2
        ]
    },
    {
        "hotel_id": "2",
        "category": [
            1,
            1,
            1,
            1
        ]
    },
    {
        "hotel_id": "3",
        "category": [
            1,
            1,
            1,
            1,
            1
        ]
    },
    {
        "hotel_id": "4",
        "category": [
            1
        ]
    }
]

我需要类别中的价格

我现在更新了我的代码,你可以查看我在做什么,是的,我使用的是reduce方法但是无法获得实际的结果。

1 个答案:

答案 0 :(得分:0)

reduce()中,您并没有突破每个类别。我不是100%确定category所需的输出,然后使用具有每个唯一值的对象作为每个类别的键和价格数组

var hotel_to_values = myarray.reduce(function(obj, item) {
   const o = obj[item.hotel_id] = obj[item.hotel_id] || {};
   o[item.category] = (o[item.category]  || []).concat(item.price); 
  return obj;
}, {});

var hotels = Object.keys(hotel_to_values).map(function (key) {
    return {hotel_id: key, category: hotel_to_values[key]};
});


console.log(hotels)
.as-console-wrapper {max-height: 100%!important;}
<script>
  let myarray = [{
      price: 257,
      category: 1,
      hotel_id: 4
    },
    {
      price: 493,
      category: 1,
      hotel_id: 3
    },
    {
      price: 514,
      category: 1,
      hotel_id: 3
    },
    {
      price: 257,
      category: 1,
      hotel_id: 3
    },
    {
      price: 104,
      category: 1,
      hotel_id: 3
    },
    {
      price: 295,
      category: 1,
      hotel_id: 3
    },
    {
      price: 125,
      category: 1,
      hotel_id: 2
    },
    {
      price: 125,
      category: 1,
      hotel_id: 2
    },
    {
      price: 157,
      category: 1,
      hotel_id: 2
    },
    {
      price: 125,
      category: 1,
      hotel_id: 2
    },
    {
      price: 125,
      category: 1,
      hotel_id: 1
    },

    {
      price: 43,
      category: 1,
      hotel_id: 1
    },
    {
      price: 43,
      category: 2,
      hotel_id: 1
    },
    {
      price: 43,
      category: 2,
      hotel_id: 1
    }
  ];
</script>