【发布时间】:2015-10-07 17:10:22
【问题描述】:
我想根据变量的值创建一个对象。我希望下面的代码可以更好地解释我想要的:
$('.tariffCostState').each(function(){
var tariffCostObj = $(this).data('tariff-code-name');
/*
How do I set the value of tariffCostObj as an object ?
[EDIT]
Imagine that var tariffCostObj='abc';
I want that abc becomes an object: abc={}
*/
var testObj={};
var tariffCostsInput=$(this).find('input.m2m-tariff-costs-input');
for(var j= 0, inputLen = tariffCostsInput.length; j<inputLen; j++){
var tariffCostsInputId = $(this).find('input.m2m-tariff-costs-input').eq(j).attr('id');
var tariffCostsInputValue= $(this).find('input.m2m-tariff-costs-input').eq(j).val();
testObj[tariffCostsInputId]=tariffCostsInputValue;
//The goal is to have:
abc[tariffCostsInputId]=tariffCostsInputValue;
}
console.log(testObj);
});
我想要tariffCostObj (abc, ... ) 而不是一个名为testObj 的对象,testeObj 只是一个示例
谢谢
【问题讨论】:
-
您的意思是可能像这样?
{valueOftariffCostObj :{....}} -
请显示预期结果对象的轮廓。不太清楚目标是什么
-
请记住,您在其中设置的对象仅在
.each()中可见。您是否也希望能够在每个外部访问它?
标签: javascript jquery object javascript-objects