【发布时间】:2020-08-04 23:39:08
【问题描述】:
我已经动态创建了下拉菜单并使用 json 数组中的数据填充它们。
数据:
data = [{hello: 'abc', asd: '123', fgh: '345' },
{hello: 'sdf', asd: '456', fgh: 'df' },
{hello: 'ty', asd: '789', fgh: '345675' },
{hello: 'qwe', asd: '123', fgh: '987' }]
键数组(用于下拉列表中我需要的数据):arr = ['asd', 'fgh']
创建下拉菜单:
arr.forEach(c => {
$('div').append(`
<div class='float-left'>
<p>${c}</p>
<select id='${c}'></select>
</div>`);
});
填充下拉列表:
arr.forEach(o => {
data.forEach(strs => {
if (strs[o] != null) {
$(`#${o}`).append(`<option value='${strs[o]}'>${strs[o]}</option>`);
}
});
});
我能够填充下拉列表。如何使用 NO DUPLICATES 填充下拉列表?
【问题讨论】:
标签: javascript jquery arrays json ecmascript-6