Home > Web Front-end > JS Tutorial > body text

A brief discussion on the application of json.stringify() and json.parse()

青灯夜游
Release: 2019-11-30 17:35:01
forward
1820 people have browsed it

This article will briefly talk about json.stringify() and json.parse(), and see the application through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on the application of json.stringify() and json.parse()

JSON.stringify() converts a js object into a JSON string, and JSON.parse() can convert a JSON string into an object js object.

Application:

//判断数组是否包含某对象
function checkObjInArray(arr,obj){
  //if(!arr) {
  //  arr = [ {name:'aaa'}, {name:'bbb'}, {name:'ccc'}, ];
  //}
  //if(!obj) {
  //  obj = {name:'ddd'};
  //}
  if(JSON.stringify(arr).indexOf(JSON.stringify(obj)) !== -1){
    return true;
  }else{
    return false;
  }

}

//判断两数组/对象是否相等
function checkObjEq(obj1,obj2){
  if(JSON.stringify(obj1) === JSON.stringify(obj2)){
    return true;
  }else{
    return false;
  }
}
Copy after login

This article comes from the js tutorial column, welcome to learn!

The above is the detailed content of A brief discussion on the application of json.stringify() and json.parse(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template