兄弟们,俺只讲核心哈
就讲修改和删除把
//修改
myobj.ke[0]= "HTML教程"
var i,x="";
for(i in myobj.ke)
{
x = x + i + " | " + myobj.ke[i] + "<br>"
}
document.getElementById("demo02").innerHTML = x;
核心在于覆盖
其他的有不懂的同学请看123哈,你真会跳啊兄dei
//删除
delete myobj.ke[2]
var j,y = "";
for(j=0;j<myobj.ke.length;j++)
{
y = y + myobj.ke[j] + "<br>"
}
document.getElementById("demo03").innerHTML = y;
核心在于删除delete myobj.ke[2],因为没有了,浏览器以为你没写就undefined啊,兄dei
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<h2>JSON 数组04-对象中的数组-修改和删除</h2>
<div id="demo01"></div><hr>
<div id="demo02"></div><hr>
<div id="demo03"></div>
<script type="text/javascript">
var myobj;
myobj={
"xm":"黄菊华",
"num":3,
"ke":["html","css","js"]
}
document.getElementById("demo01").innerHTML =myobj.ke[0]+"<br>";
document.getElementById("demo01").innerHTML = document.getElementById("demo01").innerHTML + myobj.ke[1]+"<br>";
document.getElementById("demo01").innerHTML = document.getElementById("demo01").innerHTML + myobj.ke[2]+"<br>";
//修改
myobj.ke[0]= "HTML教程"
var i,x="";
for(i in myobj.ke)
{
x = x + i + " | " + myobj.ke[i] + "<br>"
}
document.getElementById("demo02").innerHTML = x;
//删除
delete myobj.ke[2]
var j,y = "";
for(j=0;j<myobj.ke.length;j++)
{
y = y + myobj.ke[j] + "<br>"
}
document.getElementById("demo03").innerHTML = y;
</script>
</body>
</html>
兄弟们,核心来了,超级嵌套
!!!!
记住了,兄弟们,对象里面的是属性,数组里面的是下标从0开始的下标,核心哈·
比如这里面是吧·,对象里面的属性有name num sites
sites属性里面的数组下标为
1{“name”:“淘宝”,“info”:[“购物网站”,“www.taobao.com”,“电商”]},
2{“name”:“QQ”,“info”:[“综合网站”,“www.qq.com”,“综合”]},
3{“name”:“百度”,“info”:[“搜索网站”,“www.baidu.com”,“搜索”]}
记住了,我前面说过了,属性里面也可以写数组的哈
然后是对象里面的属性是name info
然后是对象里面的属性是数组
0[“购物网站”,“www.taobao.com”,“电商”]
1[“综合网站”,“www.qq.com”,“综合”]
2[“搜索网站”,“www.baidu.com”,“搜索”]
就这么简单,知道把
obj = {
"name":"网站",
"num":3,
"sites":[
{"name":"淘宝","info":["购物网站","www.taobao.com","电商"]},
{"name":"QQ","info":["综合网站","www.qq.com","综合"]},
{"name":"百度","info":["搜索网站","www.baidu.com","搜索"]}
]
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<h2>1JSON 数组05-复杂的嵌套</h2>
<div id="demo01"></div>
<script type="text/javascript">
var obj,i,j,x ="";
obj = {
"name":"网站",
"num":3,
"sites":[
{"name":"淘宝","info":["购物网站","www.taobao.com","电商"]},
{"name":"QQ","info":["综合网站","www.qq.com","综合"]},
{"name":"百度","info":["搜索网站","www.baidu.com","搜索"]}
]
}
for(i in obj.sites)//遍历sites属性中的数组有3个
{
x = x + "<h1>" + obj.sites[i].name + "</h1>"//三次
//然后是第一个()下标0)name的值是:
for(j in obj.sites[i].info)//然后是获取到"info":["购物网站","www.taobao.com","电商"]了,三次
{
x = x + obj.sites[i].info[j] + "<br>";//然后遍历要下{"name":"淘宝","info":["购物网站","www.taobao.com","电商"]},一共九次
//里面的["购物网站","www.taobao.com","电商"],之后的按照我的思路进行就行了
}
}
document.getElementById("demo01").innerHTML =x;
</script>
</body>
</html>
OK了,大家多想想就明白了,拜拜!!!!!