我有一个域数组:var domains = ["domain1", "domain2", "domain3"];
预期结果应为:
var domainData = {
type: 'combo',
name: 'domain',
width: 200,
offsetLeft: 30,
label: 'Test label',
required: true,
options: [
{text: 'domain1', value: 'domain1'},
{text: 'domain2', value: 'domain2'},
{text: 'domain3', value: 'domain3'},
]
};
这是我试图做的,但是没有用:
for(var i = 0; i < domains.length; i++) {
console.log(domains[i]);
var domainData = {
type: 'combo',
name: 'domain',
width: 200,
offsetLeft: 30,
label: 'Test label',
required: true,
options: [{
text: domains[i],
value: domains[i],
}]
};
}
您可以在此处使用代码:https://jsbin.com/vadered/edit?js,console
最佳答案
将domainData
对象移到函数循环外,然后添加options
的值:
var domainData = {
type: 'combo',
name: 'domain',
width: 200,
offsetLeft: 30,
label: 'Test label',
required: true,
options: []
};
for(var i = 0; i < domains.length; i++) {
domainData.options.push({ 'text':domains[i], 'value': domains[i] });
}