使用angularjs和Javascript将Object Array数据中的一个字段转换为另一个值

时间:2022-11-15 21:28:48

In an angularjs controller I have this code:

在angularjs控制器中,我有这个代码:

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.

它创建一个名为Items的对象数组。关键值只是实验aa,ab,ac等。当用户从下拉菜单输入数据时,我想只保存如下的值:1,2,3,4,5然后将数据导入回来该网站,将值转换回来。喜欢0 =坏; 5 =非常好。因此,对于每个具有键名ae的记录,我想将值从1,2,3,4,5转换为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.

我可以弄清楚程序流程,我需要知道的是如何使用面向对象的JavaScript引用值我猜。

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?

它给出了0,1,2,3,4等的值。所以我猜$ scope.UsedItems中的关键值只是数字。我不知道如何具体访问密钥和值数据。什么是一种简单的方法我可以在警报中显示数组的内容是什么?

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.

这将引用名称为ad的每条记录中的数据。这样我就可以找到记录中特定项目的方法。

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 个解决方案

#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

每次你只需像这样设置它,而不是检查数据[name] .af的值

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

这给你想要的结果你想要的数据[名称] .af = 3应该没问题,例子[3]就是你想要的......

#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

每次你只需像这样设置它,而不是检查数据[name] .af的值

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

这给你想要的结果你想要的数据[名称] .af = 3应该没问题,例子[3]就是你想要的......