1

I am trying to generate object elements dynamically from a loop by passing an integer in the initial i.e common prefix of the elements.

Like this:

           if(inventory.inventory_obj.length){
                obj.inventory_length = inventory.inventory_obj.length;
                for(var x=0; x < inventory.inventory_obj.length; x++){

                    obj.warehouse_+x = inventory.inventory_obj[x].warehouse;
                    obj.name_+x = inventory.inventory_obj[x].name;
                    obj.space_+x = inventory.inventory_obj[x].space;
                    obj.cost_+x = inventory.inventory_obj[x].cost;
                    obj.quantity_+x = inventory.inventory_obj[x].quantity;
                    obj.level_+x = inventory.inventory_obj[x].level;
                    obj.status_+x = inventory.inventory_obj[x].status;
                    obj.deleted_+x = inventory.inventory_obj[x].deleted;
                }
            }

Doing the above I get "Invalid left-hand side in assignment" error

I have tested the inventory.inventory_obj through console.log(inventory.inventory_obj) and verified that it has the needed values.

Other tries I have made include

                    obj.warehouse_+""+x = inventory.inventory_obj[x].warehouse;
                    obj.warehouse+"_"+x = inventory.inventory_obj[x].warehouse;
                    obj.warehouse_+x.toString() = inventory.inventory_obj[x].warehouse;
                    obj.warehouse.concat("_"+x+"") = inventory.inventory_obj[x].warehouse;
                    //Eliminating the underscore
                    obj.warehouse+x = inventory.inventory_obj[x].warehouse;

All the above failed.

Please someone help me understand what I am doing wrong.

felix cheruiyot
  • 311
  • 2
  • 17

3 Answers3

3

To create the property name dynamically, use the square bracket notation:

obj['warehouse_' + x] = nventory.inventory_obj[x].warehouse;
steveukx
  • 4,370
  • 19
  • 27
2

Your can't have + in the name obj.warehouse_+x and all other instances like that.

You need to use: obj["warehouse_" + x] for dynamic object key names.

For concatenation try using:

obj["warehouse_" + x] = obj["warehouse_" + x]  + inventory.inventory_obj[x].warehouse;

There is no concatenation operator for objects like there is for strings or numbers (+=).

Shawn31313
  • 5,978
  • 4
  • 38
  • 80
0

Although the language won't accept arithmetic names unless you are actually making a string out of it, I think you will have better semantic by using arrays instead of many simlar-named variables.

For example, if there is many indexed obj.warehouse, you should initialize it as an array:

obj.warehouse=[];

Then, to put something into it:

obj.warehouse[x] = inventory.inventory_obj[x].warehouse;

It will be easier for you to access it later because you won't have to concatenate every time you want to access a warehouse. Also, as long as there are "concatenating" accesses, debugging can be a pain whenever something is renamed.

Frederik.L
  • 5,522
  • 2
  • 29
  • 41