Need to categorize array according to specific ids

Zaheer Ahmad

Hello I working in java script & having issue to sort the values and get sum by categories right now i have hotel_id and 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]};
});

I need to sort or group by something like this

  • hotel 1
    • category 1
      • price 20
    • category 2
      • price 20 , price 30

right now my result is

[
    {
        "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
        ]
    }
]

I need prices inside the categories

I update my code now you can check what actually i am doing yes i use reduce method but can't able to get the actual result.

charlietfl

In your reduce() you aren't breaking out each category. I'm not 100% sure of output wanted for category , following uses an object with each unique value as key and array of prices for each 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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fetch table records according to array of ids?

From Dev

Add ids as array in a session and return products according to ids inside it

From Dev

Add ids as array in a session and return products according to ids inside it

From Dev

rearrange the array according to specific value

From Dev

sort a SKSpriteNode array according to a specific element

From Dev

Picking out elements of an array according to a specific rule

From Dev

Updating items in an array according to a specific sequence

From Dev

How to edit a specific pattern according to an array of patterns?

From Dev

How to convert a string to an array according to a specific separator?

From Dev

creating an array that does specific outputs according to Choices

From Dev

Need to find array of array with specific key

From Dev

Re-arrange an array according to some specific keys - PHP

From Dev

Split an array to smaller arrays according to specific patterns in c#

From Dev

How to select objects with specific IDs from a JSON array with jq?

From Dev

Mongoose how to find by array of ids and pull its specific sub document?

From Dev

JavaScript to categorize array elements and output as HTML

From Dev

Excel - categorize an entry from an array of keywords

From Dev

How to categorize two array values accordingly

From Dev

Properly categorize dom content into Array with XPath

From Dev

SQL - I need to see how many users are associated with a specific set of ids

From Dev

Sort by specific ids in ActiveRecord

From Dev

make accordion for specific ids

From Dev

Type specific IDs with FlowType

From Dev

Sorting a list of objects, according to their corresponding IDs in a Dictionary

From Dev

Java, need help creating a method to select a specific element in an array

From Dev

Need help storing specific values from a 2 dimensional array

From Dev

Need best combination of array elements for specific sum with minimum wastage

From Dev

Java, need help creating a method to select a specific element in an array

From Dev

Need help accessing the specific value of an array in a seperate method

Related Related

  1. 1

    How to fetch table records according to array of ids?

  2. 2

    Add ids as array in a session and return products according to ids inside it

  3. 3

    Add ids as array in a session and return products according to ids inside it

  4. 4

    rearrange the array according to specific value

  5. 5

    sort a SKSpriteNode array according to a specific element

  6. 6

    Picking out elements of an array according to a specific rule

  7. 7

    Updating items in an array according to a specific sequence

  8. 8

    How to edit a specific pattern according to an array of patterns?

  9. 9

    How to convert a string to an array according to a specific separator?

  10. 10

    creating an array that does specific outputs according to Choices

  11. 11

    Need to find array of array with specific key

  12. 12

    Re-arrange an array according to some specific keys - PHP

  13. 13

    Split an array to smaller arrays according to specific patterns in c#

  14. 14

    How to select objects with specific IDs from a JSON array with jq?

  15. 15

    Mongoose how to find by array of ids and pull its specific sub document?

  16. 16

    JavaScript to categorize array elements and output as HTML

  17. 17

    Excel - categorize an entry from an array of keywords

  18. 18

    How to categorize two array values accordingly

  19. 19

    Properly categorize dom content into Array with XPath

  20. 20

    SQL - I need to see how many users are associated with a specific set of ids

  21. 21

    Sort by specific ids in ActiveRecord

  22. 22

    make accordion for specific ids

  23. 23

    Type specific IDs with FlowType

  24. 24

    Sorting a list of objects, according to their corresponding IDs in a Dictionary

  25. 25

    Java, need help creating a method to select a specific element in an array

  26. 26

    Need help storing specific values from a 2 dimensional array

  27. 27

    Need best combination of array elements for specific sum with minimum wastage

  28. 28

    Java, need help creating a method to select a specific element in an array

  29. 29

    Need help accessing the specific value of an array in a seperate method

HotTag

Archive