Angularjs:无法设置undefined的属性

时间:2022-12-02 08:21:11

So I've been stuck at this problem and cant seem to find an answer.

所以我一直坚持这个问题,似乎无法找到答案。

I'm getting an error that says cannot set property of undefined.

我收到一个错误,指出无法设置undefined属性。

vm.dtr1 = {}; // maybe im initializing this wrong ?
vm.dtr1.date1 = {}; // maybe im initializing this wrong ?

for(var x=0; x<5; x++){
  vm.dtr1[x].date1 = new Date(days[x]); //getting undefined value
}

Thank you in advance.

先谢谢你。

5 个解决方案

#1


2  

You need to change it to be an array, then before assigning a property it must be initialized.

您需要将其更改为数组,然后在分配属性之前必须初始化它。

vm.dtr1 = []; // make it an array

for(var x=0; x<5; x++) {
   vm.dtr1[x] = {}; // initialize it first
   vm.dtr1[x].date1 = new Date(days[x]);
}

Or in better way

或者以更好的方式

vm.dtr1 = [];

for(var x=0; x<5; x++) {
   vm.dtr1[x] = { date1:  new Date(days[x])};
}

#2


3  

vm.dtr is an object, but you are trying to use it as an array by accessing the index. Which will obviously undefined.

vm.dtr是一个对象,但您尝试通过访问索引将其用作数组。这显然是未定义的。

You need to declare vm.dtr as an array of type dtx.

您需要将vm.dtr声明为dtx类型的数组。

#3


1  

Try following changes will suely work

尝试以下更改将suely工作

vm.dtr1 = []; // change here
//vm.dtr1.date1 = {}; // remove this

for(var x=0; x<5; x++){
  vm.dtr1[x] ={}
  vm.dtr1[x].date1 = new Date(days[x]); //
}

#4


0  

The problem is that the possible values of x are not correct keys for vm.dtr1.

问题是x的可能值不是vm.dtr1的正确键。

You may want to use for...in loop instead:

您可能希望使用for ... in循环:

for(var x in vm.dtr1) {
  if (vm.dtr1.hasOwnProperty(x)) {
    vm.dtr1[x] = new Date(days[x]);
  }
}

I still don't know what days is. So, you need to figure that out. For more details about iterating over an object, see this post.

我还是不知道几天过去了。所以,你需要弄明白。有关迭代对象的更多详细信息,请参阅此文章。

#5


0  

I think you might need to do it like this

我想你可能需要像这样做

var vm = {}; vm.dtr1 = []; 
var day = ['7/7/2012','7/7/2012','7/7/2012','7/7/2012','7/7/2012']
  vm.dtr1[x] = {};
  vm.dtr1[x].date1 = new Date(days[x]);
}

Still don't know what days is, i did is as expected input for Date constructor

仍然不知道几天是什么,我做的是Date构造函数的预期输入

#1


2  

You need to change it to be an array, then before assigning a property it must be initialized.

您需要将其更改为数组,然后在分配属性之前必须初始化它。

vm.dtr1 = []; // make it an array

for(var x=0; x<5; x++) {
   vm.dtr1[x] = {}; // initialize it first
   vm.dtr1[x].date1 = new Date(days[x]);
}

Or in better way

或者以更好的方式

vm.dtr1 = [];

for(var x=0; x<5; x++) {
   vm.dtr1[x] = { date1:  new Date(days[x])};
}

#2


3  

vm.dtr is an object, but you are trying to use it as an array by accessing the index. Which will obviously undefined.

vm.dtr是一个对象,但您尝试通过访问索引将其用作数组。这显然是未定义的。

You need to declare vm.dtr as an array of type dtx.

您需要将vm.dtr声明为dtx类型的数组。

#3


1  

Try following changes will suely work

尝试以下更改将suely工作

vm.dtr1 = []; // change here
//vm.dtr1.date1 = {}; // remove this

for(var x=0; x<5; x++){
  vm.dtr1[x] ={}
  vm.dtr1[x].date1 = new Date(days[x]); //
}

#4


0  

The problem is that the possible values of x are not correct keys for vm.dtr1.

问题是x的可能值不是vm.dtr1的正确键。

You may want to use for...in loop instead:

您可能希望使用for ... in循环:

for(var x in vm.dtr1) {
  if (vm.dtr1.hasOwnProperty(x)) {
    vm.dtr1[x] = new Date(days[x]);
  }
}

I still don't know what days is. So, you need to figure that out. For more details about iterating over an object, see this post.

我还是不知道几天过去了。所以,你需要弄明白。有关迭代对象的更多详细信息,请参阅此文章。

#5


0  

I think you might need to do it like this

我想你可能需要像这样做

var vm = {}; vm.dtr1 = []; 
var day = ['7/7/2012','7/7/2012','7/7/2012','7/7/2012','7/7/2012']
  vm.dtr1[x] = {};
  vm.dtr1[x].date1 = new Date(days[x]);
}

Still don't know what days is, i did is as expected input for Date constructor

仍然不知道几天是什么,我做的是Date构造函数的预期输入