Jump to: navigation, search

JQuery convert string to array and access array element

From w3cyberlearnings

Contents

Turn string into array

 var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
 var myaddress = address.split(',')

Get last array element using split,pop, and trim altogether

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script 
            type="text/javascript" 
            src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function(){
                var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
                var zip = address.split(',').pop().trim();
                $('div').html(zip);
            });
        </script>
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

Split the string and get the last element

  • myadd[0]: 2450 Lake Rd,
  • myadd[1]: Apt 1580
  • myadd[2]: Huntsville Tx
  • myadd[3]: 77340
  • Array.pop(): get the last element of the array
<script type="text/javascript">
      $(document).ready(function(){
           var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
           var myadd= address.split(',');
           $('div').html(myadd.pop());
       });
</script>

How to get the last array element in jQuery

  var myarray = [1,2,3,4,5];
  // last array element
  var a1= myarray[myarray.length-1];  //5
  var a2= myarray.pop();              //5
  var a3= $(myarray).last()[0];       //5
  var a4= myarray.reverse()[0];       //5

How to get the first array element in jQuery

  var myarray = [1,2,3,4,5];
  // first array element
  var b1= myarray[0];  //5             //1
  var b3= $(myarray).first()[0];       //1

Loop through array element using $.each()

<script type="text/javascript">
      $(document).ready(function(){
          var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
          var myaddr = address.split(',');
                $.each(myaddr, function(i,element){
                    alert(element);
                });
      });
</script>

Loop through array element using index

<script type="text/javascript">
       $(document).ready(function(){
                var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
                var myaddr = address.split(',');
                $.each(myaddr, function(i,element){
                    alert(myaddr[i]);
                });
       });
</script>

Loop through array element using for

<script type="text/javascript">
            $(document).ready(function(){
                var address ="2450 Lake Rd, Apt 1580, Huntsville Tx, 77340";
                var myaddr = address.split(',');
                for(var i=0; i<myaddr.length; i++) {
                    alert(myaddr[i]);
                }
            });
</script>

Access PHP array

PHP array

$allProfile = array(
    'person' => array(
        'name'  => 'Job',
        'age' => 32,
    ),
    'person' => array(
        'name'  => 'Bob',
        'age' => 42,
    ),
);

jQuery accessing array

$.each(arrayFromPHP, function (i, elem) {
    alert(elem.name);
    alert(elem.age);
});

Related Links


String and Selector

  1. Manipulate String with substr method
  2. Manipulate String Style
  3. jQuery Selector
  4. Manipulate and Access String
  5. Manipulate JSON Data
Navigation
Web
SQL
MISC
References