How to check first character in string and how to send that string into an array in Jquery?



I have an array and it contains some string values. ex: array name="All_array"

Now i want to check all values in that array for first character of a string.

if a String starts with character 'a' then move that string to array called "A_array". if a String starts with character 'b' then move that string to array called "B_array".

How to achieve this task.


var splitArrays = {}; for(var i = 0; i < All_array.length; ++i){ var firstChar = All_array[i].substr(0,1).toUpperCase(); if(!splitArrays[firstChar + '_array']) splitArrays[firstChar + '_array'] = []; splitArrays[firstChar + '_array'].push(All_array[i]); }

This will take every element in All_array and put them into an object containing the arrays indexed by the first letter of the elements in All_array, like this:

splitArrays.A_array = ['Abcd','Anej','Aali']


Here's a fiddle: <a href="http://jsfiddle.net/svjJ9/" rel="nofollow">http://jsfiddle.net/svjJ9/</a>


The code would be this:

for(var i=0; i<All_array.length; i++){ var firstChar = All_array[i].substr(0, 1).toUpperCase(); var arrayName = firstChar + "_array"; if(typeof(window[arrayName]) == 'undefined') window[arrayName] = []; //Create the var if it doesn't exist window[arrayName].push(All_array[i]); } A_array = []; //empty the array (cause you wanted to 'move')

Hope this helps. Cheers


You could do it using each() and charAt:

$.each(All_array,function(i,s){ var c = s.charAt(0).toUpperCase(); if(!window[c + '_array']) window[c + '_array'] = []; window[c + '_array'].push(s); });


  • Parse a substring?
  • Reading two Characters in C#
  • EntLib Way to Bind “Null” Value to Parameter
  • Unable to send e-mail through Java
  • Creating a DropDownList
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Django model inheritance, filtering models
  • Implicit joins and Where in Doctrine - how?
  • AndEngine Applying Transparancy to AndEngine View
  • converting text file into xml using php?
  • Apache RewriteRule redirection with url encoded
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • Parsing a CSV string while ignoring commas inside the individual columns
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to attach a node.js readable stream to a Sendgrid email?
  • Change Inet root folder for iis 7
  • Java: can you cast Class into a specific interface?
  • AES padding and writing the ciphertext to a disk file
  • Is possible to count alias result on mysql
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Weird JavaScript statement, what does it mean?
  • retrieve vertices with no linked edge in arangodb
  • How to delete a row from a dynamic generate table using jquery?
  • Benchmarking RAM performance - UWP and C#
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?