JQuery convert string to array and access array element

From CoderLearner
Jump to: navigation, search

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

// array

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

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

<seo metagenerator="jquery tutorial" title="Jquery/Ajax" metakeywords="jquery greater,jquery gt, jquery lt, less then, odd jquery,jquery find element, even jquery,jquery event, jquery mouse click, jquery double click, jquery hover, jquery string, jquery array, jquery button, jquery attriubtes" metadescription="jquery button, jquery attriubte, jquery example, jquery tutorial, jquery sample"/>