I have the following code:
if (!$scope.aa.hasOwnProperty('x')) {
$scope.aa.x = {}
data.answers.forEach(function (element, index) {
$scope.aa.x[index].c = null;
$scope.aa.x[index].r = null;
$scope.aa.x[index].text = element.text;
});
}
But it gives me an error:
TypeError: Cannot set property 'c' of undefined
Do I need to define an array for aa
and if so how can I do this?
An alternative, more idiomatic version of CD's answer is:
$scope.aa.x[index] = {
c : null,
r : null,
text : element.text
}
Yes. looks like x
is an array
and x[index]
is an object
:
$scope.aa.x = [];
data.answers.forEach(function (element, index) {
$scope.aa.x[index] = {};
$scope.aa.x[index].c = null;
$scope.aa.x[index].r = null;
$scope.aa.x[index].text = element.text;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.