0

I'm new to angularjs. In my json data there are two objects categories and sub_categories.

{
    "status": true,
    "data": {
        "categories": {
            "1": "Fasteners",
            "2": "Paints",
            "3": "Abrasives"
        },
        "sub_categories": {
            "1": [
                "Select All",
                "Nuts",
                "Bolts",
                "others"
            ],
            "2": [
                "All",
                "Industrial Paints",
                "Primer"
            ],
            "3": "3",
            "4": "4"
        }
    }
}

I need to map the sub_categories under respective categories. I can get the key values of categories and sub_categories byng-repeat="(keyCat,cat) in myData.data.categories " and ng-repeat = "(keySubCat,subCat) in myData.data.sub_categories" respectively but I'm not able to find a way to map them.

Can anyone please give a possible solution or guide me how to map these two objects.


My 2nd question is how can I implement a search to this json objects. This is not a json array so I'm not be able to use filters.

2 Answers 2

1

You can use the keyCat from the categories.

<div ng-repeat="(keyCat,cat) in myData.data.categories">
  {{cat}}
  <div ng-repeat="subCat in myData.data.sub_categories[keyCat]">
    {{subCat}}
  </div>
</div>

Although I would change the data structure to include the subcategories in the category value.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks , this is what I need. Can you give any hints on implementing search @taguenizy
On categories? I would add a ng-if with some function taking cat as an argument. <div ng-repeat="(keyCat,cat) in myData.data.categories" ng-if="someSearchLogic(cat)">
0

I'd try something like this:

var categories = Object.keys(data.categories).map(function(key) {
    var cat = {
    id: key,
      name: data.categories[key],
    sub_categories: data.sub_categories[key]
  }
  return cat;
});

Now that you transformed it into an array, you may search it easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.