Sunday 15 August 2010

jquery - How to return a value from $.each(...)? -


To prevent the Gazezian of close votes, I can emphasize that it is not related Is, answered alone,.

I'm not sure that $ Each (...) method of returning from construction to outer space Suppose we have an array of JSON objects (or arrays for that matter).

  var output = ""; $ .each (array, function (key1, value1) {output = = "[" + 1 + "] - & gt;" $ .ee (value1, function (key2, value2) {output = Key 2+ ":" + Value2 + "";});}); Console.log (output); Now, as shown in the example above, I am relying on a predeclared  output  which I am going to use for each sub-level I write this from the scope and it works. However, I can not feel that this is not the best way, instead of adding it to semi-global  output , I'd like to return something. Whether or not to return the return details in   

.each () If the returning is continuing or not a .each () to return a value, you can use it like .map () : < Pre> var output = $ map (array, work (v, i) {return "[" + + +] -> "+ $ .map (v, function (v, i) {return i + ':' + V + '';}). Join ();}). Join (); / P>



No comments:

Post a Comment