jQueryの配列操作
eachとmap
https://stackoverflow.com/a/749119
mapつかっていこうやmrsekut.icon
code:js
const ne = $('.sortable-product').map(function(idx) {
return {
productId: this.dataset.productId,
sortNo: idx
}
}).get(); // getが必要
const ne = [];
$('.sortable-product').each(function() {
ne.push({
productId: this.dataset.productId,
sortNo: this.dataset.sortNo
});
});
filterもある
https://js.studio-kingdom.com/jquery/traversing/filter
複数checkboxからcheckが付いているもののみを取得する例
code:js
$("inputtype='checkbox'").filter(":checked")
uncheck
code:js
$("inputtype='checkbox'").filter(':not(:checked)')
someとかeveryはjquery内には定義されていないが、$.makeArray(..)を使うことで、jsのそれが使える
code:js
$.makeArray($(...)).some(function(x) { ... })
似たものにtoArrayもある
code:js
$(…).toArray().some(function(node) { … })
違い
https://stackoverflow.com/a/16434875
toArray
jQueryのmethodである
つまり、jQuery Object→JS Arrayという変換を行う
適用後は、jsのarrayになる
makeArray
transforms any array like object to javascript Array.
こっちはよくわからんが、jQuery Objectだけに限らないということだろうか
jQuery ObjectにmakeArrayした結果と、toArrayをしたものは全く同じ結果になるはずmrsekut.icon