我想设置Object
动态的键名。这是我的代码:
if (fieldName == 'DEPARTMENT') {
var objCombos = [];
objCombos = $scope.getValues ('DEPARTMENT');
}
if (fieldName == 'DESIG') {
var objCombos = [];
objCombos = $scope.getValues ('DESIG');
}
$scope.getValues = function (fieldName) {
var objDepart= [];
jq.ajax({
type: "GET",
url: "/Data/getValuesWithId?fieldName=" + fieldName,
async: false,
success: function (result) {
angular.forEach(result, function (value, key) {
objDepart.push({ id: key, fieldName: value });
// This is what i expect { 'id' : 11, 'DEPARTMENT': value }
// The key of 2nd element is dynamic(DEPARTMENT, DESIG, etc)
});
}
});
return objDepart;
}
如何动态设置按键?
最佳答案
var obj = { id: key};
obj[fieldName] = value; //Use Bracket notation
objDepart.push(obj);
关于javascript - AngularJS-设置动态键名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26711415/