Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove underscores from json key value

I have a json file like

{
"Asian_Cities_Countries":[
{"name":"Beijing",
"country":"China"
},
{"name":"Ankara",
"country":"Turkey"
}
],
"European_Cities_Countries":[
{"name":"Paris",
"country":"France"
},
{"name":"Madrid",
"country":"Spain"
}
]
}

It is just a part of a json file the actual json is quite big. I am fetching this json through angularjs and displaying it my html page as

    <div class="panel-group" id="accordion">
            <div ng-repeat="key in notSorted(items) track by $index" class="panel panel-default menu-panel" ng-init="value = items[key]" style="margin-bottom:10px;">
            <a data-toggle="collapse" data-parent="#accordion" id="menu-link" href="#{{key}}">
                <div class="panel-heading panel-types">
                    <h4 class="panel-title">
                    {{key}}
  </h4>
                </div></a>
                <div id="{{key}}" class="panel-collapse collapsing menu-items">
                    <div ng-repeat="item in value">
                        <div class="row">
                            <div class="col-sm-12 col-lg-3">
                                <p class="item-name">
                                {{item.name}}
                                </p>
                            </div>
                            <div class="col-sm-12 col-lg-3">
                                <p>{{item.country}}</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

Now I want to remove underscores from the key value and replace it with blank spaces.How do i do it.

I tried {{key.replace('_',' ')}} . But it removes only the first underscore and not all of them.

like image 638
Gopal Chandak Avatar asked Jan 06 '23 11:01

Gopal Chandak


2 Answers

var app = angular.module("app",[]);

app.controller("ctrl" , function($scope){
  $scope.items = {
     "Asian_Cities_Countries":
           [
             {"name":"Beijing","country":"China"},
             {"name":"Ankara","country":"Turkey"}
           ],
      "European_Cities_Countries":
          [
             {"name":"Paris","country":"France"},
             {"name":"Madrid","country":"Spain"}
          ]
 };
  
  });

app.filter('removeUnderscores', [function() {
return function(string) {
    if (!angular.isString(string)) {
        return string;
    }
    return string.replace(/[/_/]/g, ' ');
 };
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" class="panel-group" id="accordion">
            <div ng-repeat="(key,value) in items track by $index" class="panel panel-default menu-panel" ng-init="value = items[key]" style="margin-bottom:10px;">
            <a data-toggle="collapse" data-parent="#accordion" id="menu-link" href="#{{key}}">
                <div class="panel-heading panel-types">
                    <h4 class="panel-title">
                    {{key | removeUnderscores}}
  </h4>
                </div></a>
                <div id="{{key}}" class="panel-collapse collapsing menu-items">
                    <div ng-repeat="item in value">
                        <div class="row">
                            <div class="col-sm-12 col-lg-3">
                                <p class="item-name">
                                {{item.name}}
                                </p>
                            </div>
                            <div class="col-sm-12 col-lg-3">
                                <p>{{item.country}}</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

try this. use a filter similar this

 app.filter('removeUnderscores', [function() {
return function(string) {
    if (!angular.isString(string)) {
        return string;
    }
    return string.replace(/[/_/]/g, ' ');
 };
}])

and in html

   {{key | removeUnderscores}}
like image 61
Hadi J Avatar answered Jan 19 '23 23:01

Hadi J


Seems you forgot the global modifier, try this:

key.replace(/_/g,' ')

with "g" all occurences should be replaced

like image 21
frontend_dev Avatar answered Jan 19 '23 22:01

frontend_dev