I needed to convert a jQuery set to an array to use array methods like sort and map:
const result = $('.selector').toArray().map(function (el) {
const o = $(el);
return {object: o, value: o.data('value')};
}).sort(function (a, b) {
if (a.value > b.value) return 1;
if (a.value < b.value) return -1;
return 0;
}).map(el => el.object);
As you can see the result is an array of jQuery objects.
Now I need to convert that back to a jQuery set in order to manipulate it:
$(result).appendTo('#main');
But $(result) does not quite look like $('.selector) and I am getting the following error:
TypeError: e is undefined
resultis an array. Do you need to append all theresultitems to '#main'? Also,object: eseems incorrect to me. Shouldn't it beobject: el?resultinstead. Like briosheje mentioned, it's an array. You are not supposed to use an array as selector for jQuery.$('#main').append(result);instead.resultis an array but I am passing it to$to convert it back to a jQuery object. Yes, I need to appendresultto#mainbut I need to further maniuplate thoseresultelements with jQuery methods.object: ewas typo; fixed it now.