Super simple idea of nested JSON array object, judge the content of a value change inside

My case belongs to the novice Xiaobai, and the master is cautious.

 

Often encountered data nesting and data processing, for example:

json = [

     {"name":zhangsan,"id":111,"sex":1,"time":2020/12/15,},

     {"name":李四,"id":222,"sex":0,"time":1900/1/1,},

    {"name":王五,"id":333,"sex":1,"time":1900/1/1,},

         ],

After receiving the above data format, and then want to judge that sex=0, it will be displayed as female, and sex=1 will be displayed as male; regardless of white cat or black cat, it is a good cat that can be constructed and processed

Is to rewrite the split build:

//定义新容器
var newJson=[];
ver newSex;

//循环替换对象
 for(var i=0;i<json.length;i++){ //判断原json里面的长度,进行循环修改
    //这就判断替换值咯
    if(json[i].sex == 0){
              newSex= "女"
      }else{
              newSex= "男" 
       }
      newJson.push(
                {"name":json[i].name,"id":json[i].id,"sex":newSex,"time":json[i].time,}
         );
  }

Suitable for js and VUE

 

There should be a simpler method, such as spit() and other functions, which can be done directly without writing so many lines of code. The above method can build a tree or other simpler

json = [

  

 {"name":张三,"id":111,"sex":1,"time":2020/12/15,

         "childInfo":{ 

               "name":张三人,"id":111,"sex":1,"time":2020/12/15,"childInfo":null

           }

  },

     {"name":李四,"id":222,"sex":0,"time":1900/1/1,"childInfo":null

          

           "childInfo":{ 

               "name":张三啊,"id":111,"sex":1,"time":2020/12/15,"childInfo":null

              }

  },

    {"name":王五,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

],

Similar to this nesting, then you want to become this format:

JSON = [

    {"name":张三,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

     {"name":张三人,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

    {"name":李四,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

   {"name":张三啊,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

   {"name":王五,"id":333,"sex":1,"time":1900/1/1,"childInfo":null},

],

Then do a recursive loop to solve the problem, each is split, the approximate wording of VUE, JS is the same type. This is to construct a new structure (although the method is silly)

getjson(e){//tree置换【{},{}】的JSON方法
             value;//数据来源
            var NewJSON = [];
            var i ;
        	e.map((cuttentDate ) => {
        		this.NewJSON.push( {"name":value.name,"id":value.id,"sex":value.sex,"time":value.,"childInfo":value.childInfo},);
        	    cuttentDate.childInfo!=null && cuttentDate.childInfo.length >0 ? this.getjson(cuttentDate.childInfo,cuttentDate.id) : "";
            })
            
        },

You can refer to others: https://www.cnblogs.com/panyujun/p/10084092.html

Guess you like

Origin blog.csdn.net/bbs11007/article/details/111235297