简体   繁体   中英

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. 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. Or, if myOBJ had keys that were numbers, your code would work.

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?

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:

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. 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:

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

   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.

Your individual elements of myOBJ are initially undefined.

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 .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM