JSON兩次遍歷(閉門造車版)

問題描述:現有學校全體人員json,學生依班級存放,問:如何找出某號同學?

json表:

[{
	"class":"7",
	"students":
		[{"studentid":"1",
		"studentname":"simon"},
		{"studentid":"2",
		"studentname":"lily"}]
},
{
	"class":"8",
	"students":
		[{"studentid":"3",
		"studentname":"jason"},
		{"studentid":"4",
		"studentname":"bob"}]
}
];

因網絡搜尋未果,現自行腦補兩次遍歷寫法(閉門造車,諸君見笑),後如另有他法,將完善此處。

function findstudent(id,name,arr) {   
	console.log('before delete:',arr); 	
	console.log('------------------');    
	arr.forEach(function (v,i) {        
		arr[i].students.forEach(function (v,m) {  
			console.log('v:',v);		
			if (v.studentid==id && v.studentname==name) {
				console.log('Found this sutdent yet!');
				arr[i].students.splice(m,1);
				//v.splice(m,1); 
				return;
			}
		});
	});    
	console.log('after delete password:',arr);
	return arr;
}	



猜你喜欢

转载自blog.csdn.net/lawrence_loh/article/details/80867953
今日推荐