gpt4 book ai didi

javascript - 动态键名

转载 作者:行者123 更新时间:2023-11-30 08:51:11 29 4
gpt4 key购买 nike

<分区>

我试图通过在元素的初始即公共(public)前缀中传递一个整数,从循环中动态生成对象元素。

像这样:

           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;

以上都失败了。

请有人帮助我理解我做错了什么。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com