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";

    .then(onUserComplete, onError);


Open in new window

Dinesh KumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

<script type="text/javascript" src="common/jquery-2.1.1.min.js"></script>		
<script type="text/javascript" src="common/underscore-min.js"></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));

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics ā€“ known as key performance indicators (KPIs) ā€“ for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

dont' you think!
Michel PlungjanIT ExpertCommented:
Especially since map and reduce already exist in JavaScript
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.
Dinesh KumarAuthor Commented:
Thank  you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.