Json, array method, random function, array de-weight

Source: Internet
Author: User

First, Json

1Json (JavaScript object Notation, JS tag) is a lightweight data interchange format.

2.Json Syntax rules

In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, and so on. However, objects and arrays are two of the more special and common types:
    • object is represented as a key-value pair
    • Data is separated by commas
    • Curly braces Save Object
    • Square brackets Save Array

3.JSON Key-value pair is a way to save the JS object, and the JS object is similar to the wording, key/value pairs in the combination of key names written in front and double quotation marks "" package, using a colon: delimited, and then the value:

var json = {name: ' Leo ', age:32};

  

4. You can put two numbers in one JSON

var arrurl = [' img/1.png ', ' img/2.png ', ' img/3.png ', ' img/4.png '];var arrtext = [' Picture one ', ' picture Two ', ' Picture Three ', ' picture Four '];var Imgda ta = {url: [' img/1.png ', ' img/2.png ', ' img/3.png ', ' img/4.png '],text: [' Picture One ', ' picture Two ', ' Picture Three ', ' Picture Four '};

Second, for in

The 1.for...in statement is used to loop an array or an object's properties.

2.for ... in a loop, each time the code executes, the elements of the array or the properties of the object are manipulated once.

3. Syntax

For (variable in object) {    execute code here}

4. Example

var arr = [' A ', ' B ', ' C '];for (var i in arr) {alert (arr[i]);}

  

Three, array method

1. Add

1) Arr.push (); The parameter is placed at the end of the original array, and the method returns the length of the final array;

var arr = [];alert (Arr.push (' abc ')); alert (arr);

  

2). Arr.unshift (), usage and push () are the same, except that the Unshift method is to put the parameter in front of the original array;

2. Delete

1) arr.pop (); Deletes the last item of the array, and returns the deleted element;

var arr = [' haha ', ' hehe ', ' hey ', ' eh, ', ' ah Ah ']; Alert (Arr.pop ()); alert (arr);

  

2) Arr.shift (); Deletes the first item of the array and returns the deleted element;

var arr = [' haha ', ' hehe ', ' hehe ', ' hmm ', ' ah Ah '];alert (Arr.shift ()); Arr.shift (); alert (arr);

  

3.arr.splice (): This method can realize the function of adding and deleting the array;

1) Delete: To provide two parameters, the first parameter is to delete the position of the first item, the second parameter is the number to be deleted

var arr = [' haha ', ' hehe ', ' Hey ', ' hmm ', ' ah Ah '];alert (Arr.splice (1, 2));

  

2) Add: Provide multiple parameters, the first parameter is to insert the position, the second is 0 means to delete 0, followed by the element to be inserted, can be multiple, because the deletion of 0, so return an empty array;

var arr = [' haha ', ' hehe ', ' hehe ', ' mmm ', ' Ah Ah '];alert (arr.splice (1, 0, ' oops ~ ')); alert (arr);

3) Replace: Provide multiple parameters, the first parameter is the position to be inserted, the second is the number of deletes, followed by the element to be inserted, can be multiple, return the deleted array;

var arr = [' haha ', ' hehe ', ' Hey ', ' hmm ', ' ah Ah '];arr.splice (0, 2, ' Oops! '); alert (arr);

  

4. Example of an array de-weight

var arr = [1,2,2,4,2];for (var i=0; i<arr.length; i++) {for (Var j=i+1; j<arr.length; J + +) {if (arr[i] = = Arr [j]) {Arr.splice (J, 1); j--;}}} Alert (arr);

  

Json, array method, random function, array de-weight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.