我试图通过在元素的初始即公共前缀中传递一个整数来从循环动态生成对象元素。
像这样:
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;
}
}
做上述我得到"Invalid left-hand side in assignment"
错误
我已经通过 console.log(inventory.inventory_obj) 测试了 inventory.inventory_obj 并验证了它是否具有所需的值。
我所做的其他尝试包括
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;
以上都失败了。
请有人帮我理解我做错了什么。