JSON Keys to be converted to lowercase programmatically.

In the following angularjs code, $scope.productsNew contains json array whose keys are not in lowercase, they are like  Id, Sku, Description I need them to be like id, sku,description

I need to change them to lowercase in angularjs, could you help me in this.

function storeController($scope,$http) {

    function onUserComplete(response) {        
        $scope.productsNew = [{ Id: 3, Sku: 'APL', Name: 'Apple', Description: 'Eat one every day to keep the doctor away!', Price: 20 }, 
              { Id: 4, Sku: 'BAN', Name: 'Banana', Description: 'These are rich in Potassium and easy to peel.', Price: 40 }];
    };
    
    function onError(reason) {
        $scope.error = "Could not fetch the Products";
    };

    $http.get("http://localhost:60128/Products-Service/Products")
    .then(onUserComplete, onError);

}

Open in new window


Thanks
meetDinesh
Dinesh KumarAsked:
Who is Participating?
 
ste5anSenior DeveloperCommented:
E.g.

 
<script type="text/javascript" src="common/jquery-2.1.1.min.js"></script>		
<script type="text/javascript" src="common/underscore-min.js"></script>
<script>
	$(document).ready(function() {
		var productsNew = [
			{ Id: 3, Sku: 'APL', Name: 'Apple', Description: 'Eat one every day to keep the doctor away!', Price: 20 },               
			{ Id: 4, Sku: 'BAN', Name: 'Banana', Description: 'These are rich in Potassium and easy to peel.', Price: 40 }
		];				
		
		var mapped = _.map(productsNew, 
			function(p) { 
				var result = _.reduce(p, 
					function(memo, value, key) {
						memo[key.toLowerCase()] = value;
						return memo;
					}, {} );
				return result;
			}
		);
		
		console.log(JSON.stringify(mapped, undefined, 2));
	});
</script>

Open in new window

0
 
ste5anSenior DeveloperCommented:
I would guess, they are that way for a good reason. Using your own modified keys may later collide when your objects may contain both keys in different cases.

Otherwise, I'm using _.map() in such cases.
0
 
Dinesh KumarAuthor Commented:
If you can provide _.map()  example that fits good in my scenario
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dinesh KumarAuthor Commented:
Adding underscore.js will put extra burden on site as I am using angularjs

dont' you think!
0
 
Michel PlungjanIT ExpertCommented:
Especially since map and reduce already exist in JavaScript
0
 
ste5anSenior DeveloperCommented:
Well, it's the concept, isn't it? Not the tool, which matters. As I said I'm using _,cause I'm used to it.
0
 
Dinesh KumarAuthor Commented:
Thank  you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.