javascript function returning issue -


i want return var "source" value element, when put "source" out of each function, become undefined.i want return whole source array. how that? appreciated -

function _getsource(){     var product = fpd.getproduct(true);      $(product[0].elements).each(function(i, elem) {         var source = elem.parameters['source'];     })      return source;     alert (source); } 

assuming you're after array containing source property of each element:

function _getsource(){     var product = fpd.getproduct(true);      return $(product[0].elements).map(function(i, elem) {         return elem.parameters['source'];     }).get();    // .get() turns jquery collection array } 

.map replacement .each / push combo. comes functional languages "map" function takes array, transmutes each elements, , returns new array of transmuted results.

the final .get not strictly necessary if don't mind getting array-like result rather proper array.


Comments