Array, json object, eval() function in JavaScript (4)

1, Arrays in JavaScript

  • Arrays are created using [], new Array(), or new Array(count)
  • After creating an array, we can initialize it statically or assign it dynamically
  • Common attribute of array: length
  • Common methods of array: toString(), join(), reverse(), push(), pop()
<script type="text/javascript">
    //initiate static
    var myArray = [1,2,3,45,67];
    //Get the number of arrays
    alert("myArray.length="+myArray.length);
    for(var i=0;i<myArray.length;i++){
        alert("myArray["+i+"]="+myArray[i]);
    }
    //dynamic initialization
    //5 memory spaces are allocated dynamically. Before manual assignment, the system default value is undefined
    myArray = new Array(5);
    for(var i=0;i<myArray.length;i++){
        alert("myArray["+i+"]="+myArray[i]);
    }
    //Assigning values to elements in an array
    myArray[0]=true;
    myArray[1]=1;
    myArray[2]="guoguo";
    myArray[3]= new Object();
    myArray[4]= new Object();
    myArray[5]= new Object();//Although the array length is 5 when it is declared, it can actually store more content
    for(var i=0;i<myArray.length;i++){
        alert("myArray["+i+"]="+myArray[i]);
    }

    //Common methods in array
    var myArray = [1,"abc",true,4.5,16];
    //The toString() method uses commas to connect elements in an array into strings
    alert("myArray.toString()="+myArray.toString());//myArray.toString()=1,abc,true,4.5,16
    //join method to connect each element in the array with special symbols, and finally generate a string
    var str = myArray.join("-");
    alert("str="+str);//str=1-abc-true-4.5-16
    //Invert elements in an array
    myArray.reverse();
    str = myArray.join("-");
    alert("str="+str);//str=16-4.5-true-abc-1
    //pop() returns the rightmost element of the array and deletes it
    var elt = myArray.pop();
    alert("elt="+elt+",myArray.length="+myArray.length);//elt=1,myArray.length=4
    //push() adds an element to the far right of the array
    //push() and pop() simulate the stack structure, and the last one is the first out stack
    myArray.push("end");
    str = myArray.join("-");
    alert("str="+str);//str=16-4.5-true-abc-end
</script>

2, json objects in JavaScript

  • json objects are created with {} and are usually used for data exchange between different languages. It is a lightweight data transfer format
  • java can splice the json string in the background and send it to the browser. The browser can parse the string in json format, so that java and javascript can complete the data transmission
<script type="text/javascript">
    //Creating json objects
    var user ={userCode:101,
               uerName:"guoguo",
               address:"Wally force"};
    alert("user.userCode="+user.userCode);//user.userCode=101
    alert("user.uerName="+user.uerName);//user.uerName=guoguo
    alert("user.address="+user.address);//user.address = tile
    //Elements in an array can be json objects
    var userArray = [{userCode:101,
                      uerName:"guoguo",
                      address:"Wally force"},
                     {userCode:102,
                      uerName:"haha",
                      address:"Guru Guru Nagetto"}
                    ];
    alert("userArray[1].userCode="+userArray[1].userCode);
    alert("userArray[1].uerName="+userArray[1].uerName);
    alert("userArray[1].address="+userArray[1].address);
</script>

3, eval() function in JavaScript

The eval() function in JS is used to interpret and execute ordinary strings as JS code.

<script type="text/javascript">
    var str = "var i=10;";
    //The function of eval: interpret and execute ordinary strings as js code            
    window.eval(str);
    alert("i="+i);//i=10
    //eval can convert a json string into a json object. Disadvantage: the name of a json variable is immutable
    var jsonStr = "var jsonObject = {userCode:'101',userName:'guoguo',age:'16'}";
    window.eval(jsonStr);
    alert("jsonObject.userCode="+jsonObject.userCode);//jsonObject.userCode=101
    alert("jsonObject.userName="+jsonObject.userName);//jsonObject.userName=guoguo
    alert("jsonObject.age="+jsonObject.age);//jsonObject.age=16
    //eval can convert json string into json object, and the name of json variable can be customized
    jsonStr = "{userCode:'102',userName:'haha',age:'16'}";
    //Note: the argument required for eval() function must be in "({...})" format
    var obj = eval("("+jsonStr+")");
    alert("obj.userCode="+obj.userCode);//obj.userCode=102
    alert("obj.userName="+obj.userName);//obj.userName=haha
    alert("obj.age="+obj.age);//obj.age=16
</script>

Tags: JSON Javascript Java Attribute

Posted on Wed, 01 Apr 2020 03:07:39 -0700 by Salkcin