javascript - How might I return multiple arrays with equal values from a larger array with mixed values? -


i have array after being sorted appears this:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"]; 


there 2 "a" strings, 4 "b" strings, , 3 "c" strings.

trying return 3 separate arrays, returning them 1 @ time loop, containing only matching values. so, upon first iteration, returned array appear newarr = ["a", "a"],  the second newarr = ["b", "b", "b", "b"]  and on third iteration newarr = ["c", "c", "c"].  

however, small array of predefined values, , need algorithm can perform same operation on array of unknown size, unknown elements, , unknown number of elements. (and keep in mind array sorted begin with, in context)

here's crazy code displaying unusual, , incorrect, results:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];  for(var index = 0; index < arr.length; index++) {   var test = "";   var newarr = []; // resets new array upon each iteration   var str = arr[index]; // initialized next unique index-value    for(var = index; < arr.length; i++)   {     if(arr[i] == str)     {       newarr.push(arr[k]);       test += arr[i] + " ";     }     else     {       index = i; // changing outer loop variable       break; // exiting inner loop     }   } // end of inner loop    window.alert(test);   setvalues(newarr);  } // end of outer loop  function setvalues(arrsorted) {   var here = document.getelementbyid("here");    for(var = 0; < arrsorted.length; i++)   {     here.innerhtml += arrsorted[i] + "&nbsp;";   }    here.innerhtml += "<br />";  } // end of setvalues function 

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"]; var arrays = {}; (var i=0;i<arr.length;i++) {   if (!arrays[arr[i]]) arrays[arr[i]] = [];   arrays[arr[i]].push(arr[i]); } 

this give equivalent of

arrays = {}; arrays['a'] = ['a','a']; arrays['b'] = ['b','b','b','b','b']; arrays['c'] = ['c','c','c']; 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -