<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> var DataSource = [ {"weekday":1,"course":[ {"courseId":1,"courseName":"语文","pointName":1}, {"courseId":2,"courseName":"英语","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":1,"courseName":"语文","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]}, {"weekday":2,"course":[ {"courseId":2,"courseName":"英语","pointName":1}, {"courseId":1,"courseName":"语文","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":-1,"courseName":"不安排课程","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]}, {"weekday":3,"course":[ {"courseId":3,"courseName":"数学","pointName":1}, {"courseId":2,"courseName":"英语","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":1,"courseName":"语文","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]}, {"weekday":4,"course":[ {"courseId":1,"courseName":"语文","pointName":1}, {"courseId":2,"courseName":"英语","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":1,"courseName":"语文","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]}, {"weekday":5,"course":[ {"courseId":1,"courseName":"语文","pointName":1}, {"courseId":2,"courseName":"英语","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":1,"courseName":"语文","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]}, {"weekday":6,"course":[ {"courseId":1,"courseName":"语文","pointName":1}, {"courseId":2,"courseName":"英语","pointName":2}, {"courseId":3,"courseName":"数学","pointName":3}, {"courseId":4,"courseName":"自习","pointName":4}, {"courseId":1,"courseName":"语文","pointName":5}, {"courseId":2,"courseName":"英语","pointName":6}, {"courseId":3,"courseName":"数学","pointName":7}, {"courseId":4,"courseName":"自习","pointName":8} ]} ]; /**********************************************************************************************************************/ //查询周六第8节课 var v_weekday=6; var v_pointName=8; $.each(DataSource,function(item) { if(DataSource[item].weekday==v_weekday) { $.each(DataSource[item].course,function(o) { if(DataSource[item].course[o].pointName==v_pointName) { console.log(DataSource[item].course[o]); return; } }); return; } }); /**********************************************************************************************************************/ //添加周六第9节自习课程 var o = new Object(); o.courseId = 4; o.courseName ="自习"; o.pointName =9; $.each(DataSource,function(item) { if(DataSource[item].weekday==v_weekday) { DataSource[item].course.push(o); return; } }); //输出添加完成的结果 console.log(DataSource); /**********************************************************************************************************************/ //删除一条数据 $.each(DataSource,function(item) { if(DataSource[item].weekday==v_weekday) { for (var i = 0; i < DataSource[item].course.length; i++) { var c = DataSource[item].course[i]; if (c.pointName == v_pointName) { DataSource[item].course.splice(i, 1); } } return; } }); console.log(DataSource); /**********************************************************************************************************************/ </script> </head> <body> </body> </html>
时间: 2024-10-09 14:47:27