JSON Keys to be converted to lowercase programmatically.

Dinesh Kumar
Dinesh Kumar used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.

Author

Commented:
If you can provide _.map()  example that fits good in my scenario
Senior Developer
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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

dont' you think!
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Especially since map and reduce already exist in JavaScript
ste5anSenior Developer

Commented:
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.

Author

Commented:
Thank  you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial