87,910
社区成员
发帖
与我相关
我的任务
分享
<script>
arrList =[
{"code":"这里是code1",
"text":"这里是text1"},
{"code":"这里是code2",
"text":"这里是text2"}
]
alert(arrList[1].text)
</script>
关键是你想干什么。上面的方便初始化
下面的这个代码方便生成,你可以添加任意多个obj到数组里。
<script>
arrList=[];
function objFactory(objs){
if(typeof objs!='object'){
alert('错误的参数!')
}else if(!objs){
alert('空对象')
}else{
for(var i in objs){
arrList.push(objs[i])
}
}
}
objFactory(
[
{"code":"这里是code1",
"text":"这里是text1"},
{"code":"这里是code2",
"text":"这里是text2"}
]
)
alert(arrList[1].text)
</script>