Simple format data into pit

Typically, the data is coming from the rear end of the simple format. We often put data into the data with nested relationship, it is convenient front-end rendering, data transformation, many pit to note.
Json first data format is essentially an object, the object key pair is written, for example, so {{},{},{}}. So, we want to declare variables before using the variable object var test1 = new Object();.
Second, we must pay attention to whether the data is passed in the json format? Index format and json format similar performance on Firefox, can also be found in the circulation, but in a different cycle index value is not the same, error-prone, you need to convert the format.

var test2 = JSON.parse(JSON.stringify(test1);

ps: when used in vue template can not be defined in advance test2 data in data, otherwise the format and not converted.
Finally traverse the object, a method for nested if judged by layering and changed.

for(var i=0; i<vmain.menu.length; i++){
//这是数据分层
if(vmain.menu[i].level == 1){
							//添加一个child的键
							test1["child"] = {};
							vmain.c1menu[a]= b;
						}else if(vmain.menu[i].level == 2){
							vmain.c2menu[a]= b;
						}
					};
					//这是数据拼装
					for(var i=0; i<vmain.menu.length; i++){
						if(vmain.menu[i].level == 1){
							continue;
						}else if(vmain.menu[i].level == 2){
							//这是将第二层的数据插入第一层的child中,拼装的复杂数据
							vmain.c1menu[vmain.c2menu[vmain.menu[i].id].pid].child[vmain.menu[i].id]=vmain.c2menu[vmain.menu[i].id]
						}
					};

This is a simple data conversion.

Guess you like

Origin blog.csdn.net/weixin_44724060/article/details/88633295