var temp = [{}]
var as = ['a', 'b']
for (i = 0; i < as.length; i++) {
temp[i].model = as[i]
}
I'm getting the following error:
我收到以下错误:
Cannot set property 'model' of undefined
无法设置未定义的属性“模型”
5 个解决方案
#1
2
Okay, this should do the trick for you:
好的,这应该为你做的伎俩:
var temp = [];
var as = ['a', 'b'];
for (i = 0; i < as.length; i++) {
temp[i] = temp[i] || {};
temp[i].model = as[i];
}
Because as
can be longer than temp
, you need to make sure temp[i]
exists, which is exactly what temp[i] = temp[i] || {};
does:
因为可能比temp更长,你需要确保temp [i]存在,这正是temp [i] = temp [i] || {};作用:
- If
temp[i]
exists, it does nothing. - If
temp[i]
doesn't exist, it assigns a new empty object ({}
) totemp[i]
.
如果temp [i]存在,它什么都不做。
如果temp [i]不存在,则为temp [i]分配一个新的空对象({})。
Notice that I removed the {}
from temp = [{}]
--> temp = []
. You don't need that first empty object in there, now.
请注意,我从temp = [{}] - > temp = []中删除了{}。你现在不需要第一个空对象。
If you're absolutely certain temp
will never contain any values before the loop, you can just do this instead:
如果你绝对确定temp在循环之前永远不会包含任何值,你可以这样做:
for (i = 0; i < as.length; i++) {
temp[i] = { model: as[i] };
}
Or:
temp = as.map(function(item){ return { model: item }});
#2
0
Length of temp
is 1, length of as
is 2. temp[1]
is thus undefined.
temp的长度为1,长度为2.因此temp [1]是不确定的。
I think this communicates the intention of the code better, and is correct:
我认为这更好地传达了代码的意图,并且是正确的:
var temp = [];
var as = ['a', 'b'];
for (var i = 0; i < as.length; i++) {
temp[i] = {model: as[i]};
}
#3
0
To always have enough values in temp
you could populate it beforehand:
要始终在temp中有足够的值,您可以事先填充它:
var as = ['a', 'b'];
var temp = as.map(function() { return {}; });
for (i = 0; i < as.length; i++) {
temp[i].model = as[i]
}
#4
0
Try this
var temp = [];
var as = ['a', 'b']
for (i = 0; i < as.length; i++) {
temp.push({'model':as[i]});
}
console.log(temp);
#5
0
this is very simple.
这很简单。
use this below for your problem.
使用以下内容解决您的问题。
> var temp =[[]]; var as = ['a','b'] for(i=0;i<as.length;i++){
> temp.push({model : as[i]}) }
>
> console.log(temp)
Let me know if you have any clarification
如果您有任何澄清,请告诉我
#1
2
Okay, this should do the trick for you:
好的,这应该为你做的伎俩:
var temp = [];
var as = ['a', 'b'];
for (i = 0; i < as.length; i++) {
temp[i] = temp[i] || {};
temp[i].model = as[i];
}
Because as
can be longer than temp
, you need to make sure temp[i]
exists, which is exactly what temp[i] = temp[i] || {};
does:
因为可能比temp更长,你需要确保temp [i]存在,这正是temp [i] = temp [i] || {};作用:
- If
temp[i]
exists, it does nothing. - If
temp[i]
doesn't exist, it assigns a new empty object ({}
) totemp[i]
.
如果temp [i]存在,它什么都不做。
如果temp [i]不存在,则为temp [i]分配一个新的空对象({})。
Notice that I removed the {}
from temp = [{}]
--> temp = []
. You don't need that first empty object in there, now.
请注意,我从temp = [{}] - > temp = []中删除了{}。你现在不需要第一个空对象。
If you're absolutely certain temp
will never contain any values before the loop, you can just do this instead:
如果你绝对确定temp在循环之前永远不会包含任何值,你可以这样做:
for (i = 0; i < as.length; i++) {
temp[i] = { model: as[i] };
}
Or:
temp = as.map(function(item){ return { model: item }});
#2
0
Length of temp
is 1, length of as
is 2. temp[1]
is thus undefined.
temp的长度为1,长度为2.因此temp [1]是不确定的。
I think this communicates the intention of the code better, and is correct:
我认为这更好地传达了代码的意图,并且是正确的:
var temp = [];
var as = ['a', 'b'];
for (var i = 0; i < as.length; i++) {
temp[i] = {model: as[i]};
}
#3
0
To always have enough values in temp
you could populate it beforehand:
要始终在temp中有足够的值,您可以事先填充它:
var as = ['a', 'b'];
var temp = as.map(function() { return {}; });
for (i = 0; i < as.length; i++) {
temp[i].model = as[i]
}
#4
0
Try this
var temp = [];
var as = ['a', 'b']
for (i = 0; i < as.length; i++) {
temp.push({'model':as[i]});
}
console.log(temp);
#5
0
this is very simple.
这很简单。
use this below for your problem.
使用以下内容解决您的问题。
> var temp =[[]]; var as = ['a','b'] for(i=0;i<as.length;i++){
> temp.push({model : as[i]}) }
>
> console.log(temp)
Let me know if you have any clarification
如果您有任何澄清,请告诉我