0

In an angularjs controller I have this code:

var ysshControllers = angular.module('theControllers', []);

ysshControllers.controller('CommonController',
    function($scope, $http, $route) {
        $scope.dbKey = $route.current.customInput;
        $scope.urlToDb = 'https://' + $scope.dbKey + '/.json';
        $http.get($scope.urlToDb).success(function(data) {
            var values = [];
            for (var name in data) {
                values.push(data[name]);
            }
            $scope.Items = values;
            });
            // Initially order by date and time
            $scope.orderProp = 'ac';
    }
);

It creates an object array with the name Items. The key values are just labed aa, ab, ac etc. When the user inputs data from a drop down menu, I want to save only values like: 1,2,3,4,5 and then when the data is imported back into the website, convert the values back. Like 0 = Bad; 5 = Very Good. So, for every record with the key name ae I want to convert the values from 1,2,3,4,5 to Bad, Okay, Fair, Good, Very Good.

I can figure out the program flow, what I need to know is how to reference the values using object oriented JavaScript I guess.

The data is structured like this:

C5_200630_Option 1

   aa:"Option 1"
   ab:"Toyota"
   ac:6499
   ad:"Truck"
   ae:"Tacoma, Silver, 1/2 ton 4wd"
   af:4

I ran this like of code:

alert(Object.keys($scope.UsedItems));

And it gives values of 0,1,2,3,4 etc. So I guess the key values in $scope.UsedItems are just numbers. I don't know how to access the key and value data specifically. What is a simple way I can just display in an alert what the content of the array is?

I used this line:

alert(data[name].ad);

And that will reference the data in every record with the name ad. So that gives me a way to identify a specific item in the record.

Okay, I figured out a solution:

            if (data[name].af === "3") {
                data[name].af = "Awesome!";
            }

Even though I figured out the solution to my problem, I still have almost no idea what I'm doing. So if there is a better way, let me know.

1 Answer 1

1

You can define an array like this

var example = ["Bad", "Not Bad", "Fine", "Good", "Very Good"];

and instead of checking value of data[name].af every time you can simply set it like this

data[name].af = example[data[name].af];

which gives you the result you want as you want data[name].af=3 should be fine and example[3] is what you want...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.