demo2:

Home  >  Article  >  Web Front-end  >  js handles common operations such as json and string comparison_javascript skills

js handles common operations such as json and string comparison_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:23:13946browse

js handles common operations such as insertion, modification, deletion of json format, and comparison of strings

demo 1:
Insertion and deletion of json format

Copy code The code is as follows:

















Running result:
js handles common operations such as json and string comparison_javascript skills
demo2:
Copy code The code is as follows:










< ;input type="button" value=">" onclick="toRight();submit()"/>











Run results:
js handles common operations such as json and string comparison_javascript skills
Demo 3:

Insertion, modification, deletion of json elements, and conversion format to string
Copy code The code is as follows:



IT-Homer demo< ;/title> <br></head> <br><br><body> <br><input type="button" id="parse" value="parse json" onclick="parseJson() " /> <br><div id="txt" /> <br><br><script type="text/javascript"> <br>function parseJson(){ <br>var txt = document.getElementById("txt"); <br><br>var jsonData = '{"name":"it-homer","age":25}'; <br>var field = ""; <br> <br>if(jsonData.length <= 0){ <BR>jsonData = '{}'; <BR>} <br><br>var jsonObj = JSON.parse(jsonData); // ok <BR> // var jsonObj = eval('(' jsonData ')'); // ok <BR>// var jsonObj = jsonData.parseJSON(); // error <BR>field = "name = " jsonObj.name; <BR>field = ", age = " jsonObj.age; <br><br>jsonObj["sex"] = "boy"; // add json, {"name":"it-homer","age": 25,"sex":"boy"} <BR>// createJson(jsonObj, "sex", "boy"); <br><br>jsonData = JSON.stringify(jsonObj); // ok <BR>/ / jsonData = jsonObj.toJSONString(); // error <br><br>var sex = ""; <BR>if(jsonObj.length > 0) { <br>sex = jsonObj.sex; <br>} <br><br>txt.innerHTML = field "</br>" jsonData; <br>} <br><br>function createJson(jsonObj, key, value){ <br>if(typeof value === "undefined"){ <br>delete jsonObj[key]; <br>} else { <br>jsonObj[key] = value; <br>} <br>} <br></script> <br>< ;/body> <br></html> <br> </div> <br>Run results: <br><img src="http://files.jb51.net/file_images/article/201309/2013090816014516.gif?20138816156" alt="js handles common operations such as json and string comparison_javascript skills" > <br>Demo4: <br><br>Compare the different elements of the two strings and Print it out. The two strings have an inclusion relationship, that is, one string must be a substring of the other string. For example, {1,2,4} is a substring of {1,2,3,4,5}<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="73695" class="copybut" id="copybut73695" onclick="doCopy('code73695')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code73695"> <br><html> <br><head> <br><title>IT-Homer demo










Running results:
js handles common operations such as json and string comparison_javascript skills
Demo5:
Copy code The code is as follows:



hello









我,喜,欢

,你

,做,朋,友










运行结果:
js handles common operations such as json and string comparison_javascript skills
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn