获取 HTMLCollection 的某个属性

Get certain attribute of HTMLCollection

所以我想知道是否有办法获得例如HTMLCollection 的所有 value。所以像这样:

var users = document.getElementsByName('users')
var userLength = []
for (var i = 0; i < users.length; i++) {
    userLength.push(users[i].value);
}
userLength.sort(function(a, b){return b - a});
console.log(userLength);

...但是在一行中,更像这样:

document.getElementsByName('users').value

如果你想运行这个,那是为sites-section on stackexchange写的。不,第二个不起作用。

我不能使用 jQuery,所以 this 不适合我。

首先获取元素数组,使用Spread syntax (...), then use Array.prototype.map()获取所有值。最后在返回的结果上链接 sort 方法:

var users = document.getElementsByName('users')
var userLength = [...users].map(el => +el.value).sort(function(a, b){return b - a});
console.log(userLength);
<input name="users" value="11"/>
<input name="users" value="22"/>