Group javascript object by category

Asked

Viewed 475 times

0

I have an object in JS with information of places (Cinema, Museum, etc.) that I will use to popular in my system. Example:

    var places = {
        "type": "ABC",
        "features": [ 
            {
                "type": "AB",
                "properties": {
                    "title": "Text 1",
                    "description": "Lorem ipsum 1",
                    "category": "Museu"
                },
                "optional": {
                    "type": "ZX",
                }
            },
            {
                "type": "AB",
                "properties": {
                    "title": "Text 2",
                    "description": "Lorem ipsum 2",
                    "category": "Museu"
                },
                "optional": {
                    "type": "ZX",
                }
            },
            {
                "type": "AB",
                "properties": {
                    "title": "Text 3",
                    "description": "Lorem ipsum 3",
                    "category": "Hotel"
                },
                "optional": {
                    "type": "ZX",
                }
            }
        ]
    }

But now I need to break it into an object dividing by category, basically staying that way:

    var variavel = {
        {
            category: "Museu",
            places: {
                type: "ABC",
                features: [ 
                    {
                        "type": "AB",
                        "properties": {
                            "title": "Text 1",
                            "description": "Lorem ipsum 1",
                            "category": "Museu"
                        },
                        "optional": {
                            "type": "ZX",
                        }
                    },
                    {
                        "type": "AB",
                        "properties": {
                            "title": "Text 2",
                            "description": "Lorem ipsum 2",
                            "category": "Museu"
                        },
                        "optional": {
                            "type": "ZX",
                        }
                    },
                ]
            }
        },
        {
            category: "Hotel",
            places: {
                type: "ABC",
                features: [ 
                    {
                        "type": "AB",
                        "properties": {
                            "title": "Text 3",
                            "description": "Lorem ipsum 3",
                            "category": "Hotel"
                        },
                        "optional": {
                            "type": "ZX",
                        }
                    },
                ]
            }
        }
    }

What is the best way to achieve this? In this project there is no ES6.

  • 1

    What you’ve tried to do?

  • Already have examples of many answers here with ES6, using reduce, map, findIndex among others. You have to use the same logic and transform these functions into their respective links.

1 answer

2


Man I developed my reasoning on ES6, IE, if you want to implement the way I did just convert the code to ES5 (There are several methods to do this in a very simple way)..

Below is the way I thought to solve the problem

** function receives as parameter a variable 'data' that represents the data structure that Voce showed in your question

function filterPlacesByCategory( data ){
    const categories = getAllCategories()
    const filteredData = breakAndBuildTheNewStructure()
    return filteredData

    function getAllCategories(){
        // pega todas as categorias
        const allCategories = data.features.map((item)=>{
            return item.properties.category
        })
        // remove as categorias repetidas
        const categoriesWithoutRepeatData = allCategories.filter(function(item, index, self) {
            return self.indexOf(item) == index;
        })

        return categoriesWithoutRepeatData
    }

    function breakAndBuildTheNewStructure(){
        return categories.map((category)=>{
            // para cada categoria ele verifica quais lugares tem aquela categoria e os retorna
            const placesWithCorrespondentCategory = places.features.filter((item)=>{
                return item.properties.category == category
            })
            // reconstroi a base dos dados ja filtrados
            return {
                "category": category,
                "places": placesWithCorrespondentCategory
            }

        })
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.