javascript arrays and closures


Earlier I posted a question about accessing array values stored in functions,<br /><a href="https://stackoverflow.com/questions/15212604/passing-array-value-using-closure/15212675#15212675" rel="nofollow">passing array value using closure</a>, and was told that this code would would do the job.

<script> function ArrValues(arr) { var arr1 = arr=='one' || arr=='all' ? ['grapes','peaches','plums'] : [], arr2 = arr=='two' || arr=='all' ? ['car','motorcycle','tree'] : [], arr3 = arr=='three' || arr=='all' ? ['200','1000','350'] : []; gotIt(arr1,arr2,arr3); } function gotIt(arr1,arr2,arr3){ alert(arr1); alert(arr2); alert(arr3); } </script>

I've updated the code as shown below. Would this be a good method of returning only the array I want without sending it as an argument to another function?

<script> function ArrValues(arr) { var arr1 = arr=='one' || arr=='all' ? ['grapes','peaches','plums'] : []; if(arr1.length>0){ return arr1; }; arr2 = arr=='two' || arr=='all' ? ['car','motorcycle','tree'] : []; if(arr2.length>0){ return arr2; }; arr3 = arr=='three' || arr=='all' ? ['200','1000','350'] : []; if(arr3.length>0){ return arr3; } } </script> <div id="one" onclick="ArrValues(this.id)">one</div> <div id="two" onclick="ArrValues(this.id)">two</div> <div id="three"onclick="ArrValues(this.id)">three</div> <div id="all"onclick="ArrValues(this.id)">all</div>


Not really. Since you're not interested in the "else" case, you would be better off with this:

if( arr == "one" || arr == "all") return ['grapes','peaches','plums'];

And similar constructs.

That being said, if you do this then the so-called "all" button will only ever return the "one" array, because it will always match the first if statement and return the array.


How about

function ArrValues(arr) { var values = { 'one': ['grapes','peaches','plums'], 'two': ['car','motorcycle','tree'], 'three': ['200','1000','350'] }; return values[arr]; }

(<em>it does not handle the all case as you do not describe what to return in this case..</em>)

<hr />

if with all you want all values merged in a single array then

function ArrValues(arr) { var match, values = { 'one': ['grapes','peaches','plums'], 'two': ['car','motorcycle','tree'], 'three': ['200','1000','350'] }; if (arr!=='all'){ match = values[arr]; } else { match = values['one'].concat( values['two'] ).concat( values['three'] ); } return match; }


  • Why are “double braces” needed in declaration of multi-dimensional array using stacked std::array?
  • Combine multiple array to another array [duplicate]
  • merging multiple numpy arrays
  • How can I dynamically allocate 2D-array in one allocate C
  • Deleting an item from a list using the DOM
  • Passing variable from 1st page to 3rd page in PHP [duplicate]
  • Merge and sum two multidimensional arrays in PHP
  • Append array values to another array by keys comparison
  • Cannot store Object in List with method from package?
  • PHP COUNT_RECURSIVE only count non-array values?
  • Pairwise subtraction of two arrays
  • Cached images loaded again in Lazyload
  • Merging 2 arrays with different value types
  • Find a number before another specific number on a vector
  • How to loop through multiple arrays to find something common
  • Javascript, map returns undefined
  • Spring Batch thread-safe Map job repository
  • How to update data into a file in a particular position in js
  • Runtime error in UVA Online Judge [closed]
  • How to Divide an array on c#?
  • JSON - slashes not escaping
  • Oracle - Second level subquery cannot see field from main query
  • Changing references to deprecated methods C++
  • Inversing an interpolation of rotation
  • Configure nginx to return different files to different authenticated users with the same URI
  • How does this usort cmp function actually work?
  • Copy to all folders batch file?
  • D3 nodes and links from JSON with nested arrays of children
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?