2 回答
TA贡献1853条经验 获得超6个赞
假设 days 是选项的子集,并且您希望所有选项数组值都按照天数的项目索引进行排序。并且选项数组中的任何额外项目最终都会按照它们的出现顺序出现。
这是代码
var options = ["day", "yearly","week", "sixmonth",'month',"newMonth"];
var days = ['day','week','month','yearly','sixmonth']
var sorted=days.filter(val=>options.includes(val))
options = options.filter(val => !days.includes(val))
sorted.push(...options)
console.log(sorted)
var s = document.getElementById("selectCity");
for (var i = 0; i < sorted.length; i++) {
s.innerHTML += `<option value=${i}> ${sorted[i]}</option>`;
}
TA贡献1812条经验 获得超5个赞
我认为选项是天的子集,并且您希望对选项进行排序,使其与天具有相同的顺序。
这是一个例子:
var options = ["day", "yearly","week", "sixmonth",'month'];
var days = ['day','week','month','yearly','sixmonth'];
var sortedOptions = options.sort((a, b) => days.indexOf(a) - days.indexOf(b))
var s = document.getElementById("selectCity");
for (var i = 0; i < sortedOptions.length; i++) {
s.innerHTML += `<option value=${i}> ${sortedOptions[i]}</option>`;
}
添加回答
举报