Link to home
Create AccountLog in
Avatar of Dinesh Kumar
Dinesh KumarFlag for India

asked on

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
Avatar of ste5an
ste5an
Flag of Germany image

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.
Avatar of Dinesh Kumar

ASKER

If you can provide _.map()  example that fits good in my scenario
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Adding underscore.js will put extra burden on site as I am using angularjs

dont' you think!
Especially since map and reduce already exist in JavaScript
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.
Thank  you.