Home  >  Article  >  Web Front-end  >  A brief discussion on the application of json.stringify() and json.parse()

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

青灯夜游
青灯夜游forward
2019-11-30 17:34:481786browse

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;
  }
}

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete