+
50
-

js 对象的属性名如果是变量如何设置和获取值?

js 对象的属性名如果是变量如何设置和获取值?

高手回复

+
0
-
<script type="text/javascript">
var obj = {
attr: {}
};
var egData = ['oneAttr', 'twoAttr', 'threeAttr'];
for (var i = 0; i < egData.length; i++) {
obj.attr[egData[i]] = false
}
console.log(obj);

var newobj={test1:123,test2:456};
for (var i = 1; i < 3; i++) {
var attrname="test"+i;
newobj[attrname] = "rrrrrrrrr";
}
console.log(newobj);
</script>

by ppzhuzhu
我知道答案,我要回答