[英]Javascript : Adding Key Value pair literal giving undefined result
it works on my last test but now its giving me hard time.. basically i have this code : 它可以在我的最后一个测试上工作,但是现在给了我很多时间。
var myOBJ = {};
for (var i = 0; i < myitems.length; i++) {
myOBJ[i].itemtype = myitems.type;
myOBJ[i].name = myitems.name;
}
inside myitems
is a data that I need to rewrite to another object
for some reason. 在
myitems
里面是出于某种原因我需要重写到另一个object
的数据。 but I'm stock with the error im having 但我有错误我有
Error: myOBJ[i] is undefined
could anyone tell my what im missing? 谁能告诉我我错过了什么?
You cannot access an object literal by index, you must access it by key . 您不能按索引访问对象文字,必须按键访问它。
If myOBJ
were an array, then you could access it by index. 如果
myOBJ
是一个数组,则可以按索引访问它。 Or, if myOBJ
had keys that were numbers, your code would work. 或者,如果
myOBJ
具有数字键,则您的代码将起作用。
You can make each key a number pretty easily, and then be able to access each object by sort of a pseudo index, if you instantiate each object first. 您可以很容易地使每个键成为一个数字,然后,如果您首先实例化每个对象,则可以通过某种伪索引来访问每个对象。
myOBJ[i] = {};
This will output something like: 这将输出类似:
{
1: {};
//etc.
}
But why not simply make myOBJ
an array? 但是,为什么不简单地使
myOBJ
成为数组呢?
You have to intiliase an object inside every element you are trying to create, so basically do this: 您必须在要尝试创建的每个元素中整合一个对象,因此基本上可以这样做:
var myOBJ = {};
for (var i = 0; i < myitems.length; i++) {
// Create a new object here.
myOBJ[i] = {};
myOBJ[i].itemtype = myitems.type;
myOBJ[i].name = myitems.name;
}
And since you are working numerically anyway, it might be better to create myOBJ
as a numerical array: 而且由于无论如何都在数值上工作,因此最好将
myOBJ
创建为数值数组:
var myOBJ = [];
Now I don't know how your myitems
object works, but you can't actually iterate over it numerically if it contains two keys called name
and type
, as its an object so it does not have a length
property. 现在,我不知道您的
myitems
对象是如何工作的,但是如果它包含两个名为name
和type
键作为对象,则实际上不能对其进行数字迭代,因此它没有length
属性。 So you might get an error there. 因此,您可能会在此处收到错误消息。 Assuming its actually an array with nested objects that contain
type
and name
you can easily iterate over it an use push()
to add an object literal
to your array, like this: 假设它实际上是一个包含嵌套对象的数组,这些对象包含
type
和name
,则可以使用push()
轻松地对其进行迭代,以向该数组添加object literal
,如下所示:
var myOBJ = [];
for (var i = 0; i < myitems.length; i++) {
// Create and push new object here.
myOBJ.push({
itemtype : myitems[i].type,
name : myitems[i]. name
});
}
If you just want to copy values from myitems
to another object in myOBJ
, I suggest you change myOBJ
to an Array
, create a temp object inside loop and add that temp object to your myOBJ array 如果你只是想从复制值
myitems
到另一个对象myOBJ
,我建议你换myOBJ
一个Array
,创建内部环路一个临时对象和临时对象添加到您的MyObj中数组
var myOBJ = [];
for (var i = 0; i < myitems.length; i++) {
var item = myitems[i];
var temp = {
'itemtype' : item.type,
'name' : item.name
};
myOBJ[i] = temp; // (or) myOBJ.push(temp);
}
You can even retain myOBJ = {}
but then again it will be just an object with each property being a Number
which is semantically identical to an Array, so use an Array instead. 您甚至可以保留
myOBJ = {}
但是它仍然只是一个对象,每个属性都是一个Number
,在语义上与Array相同,因此请改用Array。
Your individual elements of myOBJ
are initially undefined. 最初未定义
myOBJ
各个元素。
You must either create a new object to contain the properties: 您必须创建一个新对象来包含属性:
myOBJ[i] = {};
myOBJ[i].itemtype = myitems.type;
myOBJ[i].name = myitems.name
or, perhaps more efficiently, just initialise it with an object literal: 或者,也许更有效地,只需使用对象文字对其进行初始化:
myOBJ[i] = {
itemtype: myitems.type,
name: myitems.name
};
Since you only appear to be using numeric indices you should also consider using an array []
instead of an object {}
for the variable myOBJ
. 由于您似乎只使用数字索引,因此还应该考虑使用数组
[]
代替变量myOBJ
的对象{}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.