js json 特定条件删除 增加 遍历

    <script type="text/javascript">
        //直接声明json数据结构
        var myJSONObject = [
            { "ircEvent": "PRIVMSG1", "method": "newURI" },
            { "ircEvent": "PRIVMSG2", "method": "deleteURI" },
            { "ircEvent": "PRIVMSG3", "method": "randomURI" }
        ];
        //JSON.parse("{\"data\":[]}");
        //t[0].a = 11; //改
        //t.remove(0); //删
        //delete myJSONObject[0];
        //alert(t[0].a.c);
        //myJSONObject.splice(0,2);
        debugger;

        //删除特定条件的Json数组
        for (var i in myJSONObject) {
            for (var k in myJSONObject[i]) {
                if (myJSONObject[i][k] == "PRIVMSG1") {
                    delete myJSONObject[i];
                    break;
                }
            }
        }

        //向数组中添加
        var arr = {
            "ircEvent": "PRIVMSG4",
            "method": "addURI"
        };
        myJSONObject.push(arr);

        //遍历Json对象
        var str = "";
        for (var i in myJSONObject) {
            str += myJSONObject[i]["ircEvent"] + "|" + myJSONObject[i]["method"] + ";";
        }
        alert(str);
    </script>

posted @ 2015-03-26 16:24  海贼王&汉库克  阅读(5132)  评论(0编辑  收藏  举报