数组排序字母顺序到下拉列表javascript

时间:2016-03-17 18:08:48

标签: javascript arrays sorting

我有一个空的下拉列表和一个数组但是我想按字母顺序在我的下拉列表中显示我的数组。我怎样才能实现这一目标?

<select id="dropdown">
</select>

var array = ["vintage","frames","treats","engraved", "stickers", "jewelerybox", "flask"];

1 个答案:

答案 0 :(得分:0)

var array = ["vintage","frames","treats","engraved", "stickers", "jewelerybox", "flask"];

array.sort(function(val1 , val2){
  return val1.localeCompare(val2);
});

console.log(array); // ["engraved", "flask", "frames", "jewelerybox", "stickers", "treats", "vintage"]