怎么判断JSON数组总是否包含某条或多条JSON数据?

 var arr1 = [ { "name" : "心", "id" : "1" }, { "name" : "肝",
                 "id" : "2" }, { "name" : "脑袋", "id" : "3" }, { "name" : "屁股",
                  "id" : "4" } ];
 var arr2 = [ { "name" : "心", "id" : "1" }, { "name" : "肝",
                 "id" : "2" }];

判断arr1 与arr2 的id相同时 arr1 的背景色改变

for(var index2 in arr2){
for(var index1 in arr1){
if(arr2[index2].id==arr1[index1].id){
$("#"+arr1[index1].id).css({"background-color":"#98bf21"});
}
}
}
用这个。上面的写错了,没法修改。坑爹的CSDN.

应该需要前端js遍历标识

for(var obj2 in arr2){
for(var obj1 in arr1){
if(obj2.id==obj1.id){
$("#"+obj1.id).css({"background-color":"#98bf21"});
}
}
}
http://blog.csdn.net/songylwq/article/details/6047609

for(var index2 in arr2){
for(var index1 in arr1){
if(arr[index2].id==arr1[index1].id){
$("#"+arr1[index1].id).css({"background-color":"#98bf21"});
}
}
}

这是我js的代码

 /* 根据id修改用户信息 pjc 2016.4.21 */
function updateEd100(id) {

    $.ajax({
        type : "POST",
        url : "/miaolangzhong/manage/forAjax.do?requestType=6",
        data : "id=" + id, // 发送到服务器的数据
        success : function(msg) {
            alert(msg);
            var json = eval('(' + msg + ')');
            document.getElementById("addEd100").style.display = "block";
            /* 获取后台传来的json是数组 循环获取数据 */
            $.each(json, function(index, item) {
                document.getElementById("id1").value = json[index].id;
                document.getElementById("name1").value = json[index].name;
                document.getElementById("desc1").value = json[index].desc;
                /* 获取疾病对应穴位 */
                var arr2 = eval(json[index].ear_acupoint);
                var arrStr2 = '';
                $.each(arr2, function(index, item) { // 获取后台传来的json是数组
                    // 循环拼接字符串
                    // arrStr2 += arr2[index].earName;
                    arrStr2 += "<input type='button' id='"
                            + arr2[index].acupoint_num + "' value='"
                            + arr2[index].earName + "' name='earName'/>";

                });
                // document.getElementById("earName1").value = arrStr2;
                var td2 = document.getElementById("xueweiTd2");
                var div2 = document.getElementById("xuewei2");
                div2.innerHTML = arrStr2;
                td2.appendChild(div2);
                /* 获取所有穴位 */
                var arr = eval(json[index].xuewei);

                /*
                 * var arr = [ { "name" : "心", "id" : "1" }, { "name" : "肝",
                 * "id" : "2" }, { "name" : "脑袋", "id" : "3" }, { "name" : "屁股",
                 * "id" : "4" } ];
                 */
                var retStr = ",";
                $('#xueweiList').append(
                        $(arr).map(
                                function() {
                                    return '<li id=' + this.acupoint_num
                                            + '><a href="#" >' + this.name
                                            + '</a></li><li>'
                                }).get().join(''));
                $("#nav>ul>li").click(
                        function() {
                            var focus = $(this).toggleClass('h_nav_over')
                                    .hasClass('h_nav_over');
                            if (focus)
                                retStr += this.id + ',';
                            else
                                retStr = retStr.replace(',' + this.id + ',',
                                        ',');

                            $("#xueweis").val(retStr.replace(/^,|,$/g, ''));
                        });

                for(var index2 in arr2){
                    for(var index in arr){
                    if(arr2[index2].acupoint_num==arr1[index1].acupoint_num){
                    $("#"+arr[index1].acupoint_num).css({"background-color":"#98bf21"});
                    }
                    }
                    }



            });

        }
    });

}

这是我的本体结构的代码

 div id="addEd100"
    style='display: none; z-index: 11; left: 0px; top: 0px; width: 100%; height: 100%; position: absolute;'>
    <form action="/miaolangzhong/manage/forJsp.do?requestType=16"
        method="post">
        <table width="99%" border="0" cellpadding="0" cellspacing="0"
            class="CContent">
            <tr>
                <th class="tablestyle_title">世界卫生组织推荐</th>
            </tr>
            <tr>
                <td class="CPanel">

                    <table border="0" cellpadding="0" cellspacing="0"
                        style="width: 100%">
                        <TR>
                            <TD width="100%">
                                <fieldset style="height: 100%;">
                                    <legend>世界卫生组织推荐</legend>
                                    <table border="0" cellpadding="2" cellspacing="1"
                                        style="width: 100%">
                                        <tr>
                                            <input type="hidden" name="id1" id="id1" onblur="Checkname()" />

                                            <!-- 编辑后跳转到当前的列表页 -->
                                            <input type="hidden" value=${page.num } id="currentPageNum2"
                                                name="currentPageNum2">
                                            <!-- 添加后跳转到最后页面 -->
                                            <input type="hidden" value=${page.count } id="pageCount"
                                                name="pageCount">
                                            <td nowrap align="right" width="15%">疾病名称:</td>
                                            <td width="35%"><input name='name' type="text"
                                                id="name1" class="text" style="width: 154px" value="" /> <span
                                                class="red">*</span></td>

                                        </tr>
                                        <tr>
                                            <td width="15%" nowrap align="right" height="100">简介:</td>
                                            <td colspan="3"><textarea name="desc" cols="100"
                                                    id="desc1" rows="10"></textarea></td>
                                        </tr>
                                        <tr>
                                            <td width="15%" nowrap align="right" height="100">对应的穴位:</td>
                                            <td colspan="3" id="xueweiTd2"><div id="xuewei2"
                                                    class="h_nav_over"></div></td>
                                        </tr>
                                        <tr>
                                            <td width="15%" nowrap align="right" height="100">取穴:</td>
                                            <td colspan="3" id="xueweiTd">
                                                <div id="nav">

                                                    <ul id="xueweiList"></ul>
                                                    <input id="xueweis" name="xueweis">

                                                </div> 
                                            </td>

                                        </tr>
                                    </table>
                                    <br />
                                </fieldset>
                            </TD>
                        </TR>
                    </TABLE>


                </td>
            </tr>
            <TR>
                <TD colspan="2" align="center" height="50px"><input
                    type="submit" name="Submit" value="保存" class="button"
                    onclick="alert('保存成功!');" /> <input type="button" name="Submit2"
                    value="返回" class="button" onclick="window.history.go(-1);" /></TD>
            </TR>
        </TABLE>
    </form>
</div>

取穴里面 心 肺 气管 艇中 背景色还是没有改变
图片说明

那不是直接遍历arr2就好了,里面的项目都一样,不需要参考arr1

    var arr2 = [{ "name": "心", "id": "1" }, {
        "name": "肝",
        "id": "2"
    }];
    for (var i in arr2) {
        $('#xxxxxx'+arr2[i].id).css('background','red')
    }

图片说明