JQuery: turn array input values ​​into string optimization

using jQuery I have the following code:

var selectedIdsArray = $("#selectId option:selected").map(function(){return this.value;});
var selectedIdsStr = $.map(selectedIdsArray, function(val){ return "" + val + "";});

It successfully retrieves an identifier string, for example. selectedIdsStr = "2.45,245.1" from the item <select multiple='multiple'>. I was wondering if there is a more efficient way (less code) to achieve this?

Thanks!

+3
source share
3 answers

You can change the second line as follows:

var selectedIdsStr = selectedIdsArray.get().join(',');
+11
source
var selectedIdsStr = $("#selectId option:selected").map(function(){
   return $(this).val();
}).get().join(",");

adapted from http://docs.jquery.com/Traversing/map#callback

+3
source

:

var selectedIdsStr = selectedIdsArray.get().toString()
0

Source: https://habr.com/ru/post/1713351/


All Articles