public class JsonModel { public int ID { get; set; } public string Select { get; set; } public string Input { get; set; } }
public ActionResult Test() { string json ="[{\"id\":\"1\",\"select\":\"1\",\"input\":\"aa\"},{\"id\":\"2\",\"select\":\"1\",\"input\":\"aa\"},{\"id\":\"3\",\"select\":\"1\",\"input\":\"bb\"}]"; var list = JsonConvert.DeserializeObject<List<JsonModel>>(json); var idList = list.GroupBy(m => new { m.Select, m.Input }).Select(m => m.FirstOrDefault().ID); list = list.Where(m => !idList.Contains(m.ID)).ToList(); string newJson = JsonConvert.SerializeObject(list); return View(); }
参考一下吧..
恩恩,好的,可以做到
var tbl = $("#mytable tr"); //jquery 获取所有 tr 对象
var arr = new Array(); //存放 tr 对象
var arr2 = new Array(); //存放 tr 对象 (这两个数组对象用来将来用来筛选)
var arrIndex = new Array(); //存放已经有重复对象的索引
var result = new Array(); //遍历的结果数组
for (var i = 0; i < tbl.length; i++) {
var curTr = tbl[i];
var tds = $(curTr).find('td'); //找出 tr 下的 td 对象
var id = $(tds[0]).html(); //获取ID
var select = $(tds[1]).find('select option:selected').val(); //获取Select值
var input = $(tds[2]).find('input').val(); //获取 input 值
var tdObj = { 'id': id, 'select': select, 'input': input }; //转换成 js 对象
//把每行对象添加到数组中
arr.push(tdObj);
arr2.push(tdObj);
}
//这个辅助方法用来 筛选
var digui = function (obj, arr) {
var ids = obj.id;
for (var i = 0; i < arr.length; i++) {
var curObj = arr[i];
if (obj.id != curObj.id && obj.select == curObj.select && obj.input == curObj.input) { //判断有重复对象
arrIndex.push(i); //记录索引
ids += "," + curObj.id; //拼接ID
}
}
if (ids.length > 1) { //有重复记录,数组添加到结果中
result.push(ids);
}
}
for (var i = 0; i < arr.length; i++) { //遍历 arr 中所对象有的
var curObj = arr[i];
var isExists = false; //标识
for (var j = 0; j < arrIndex.length; j++) {
if (arrIndex[j] == i) {
isExists = true;
break;
}
}
if (!isExists) {
digui(curObj, arr2);
}
}
//遍历 result 输出结果
for (var m = 0; m < result.length; m++) {
var o = result[m];
alert("id "+ o+" 是数据重复")
}
}
我用 js 自己实现了一下,这样做效率是否有点低
@快乐的开发者:
<script type="text/javascript"> $(function() { var arr = [{ "id": "1", "select": "1", "input": "aa" }, { "id": "2", "select": "1", "input": "aa" }, { "id": "3", "select": "1", "input": "bb" }]; var repeatArr = getRepeat(arr); for (var i = 0; i < repeatArr.length; i++) { var id = repeatArr[i].id; var select = repeatArr[i].select; var input = repeatArr[i].input; document.write("重复的是id="+id+",select="+select + ",input=" + input + "<br>"); } }); function getRepeat(arr) { var result = [], hash = {}; for (var i = 0; i < arr.length ; i++) { var elem = arr[i].select+','+arr[i].input; if (!hash[elem]) { hash[elem] = true; } else { result.push(arr[i]);//如果已经存在,则记录起来 } } return result; } </script>
你试下这个。
@李丶GuanYao: 这样只能找到重复的id, 需求要把几个重复对象的id 都取出来,给用户做下提示,如果代码写成这样 只会知道有哪个id 重复了,不知道他们几个重复,和谁重复了。
1 $(function() { 2 var arr = [{ "id": "1", "select": "1", "input": "aa" }, 3 { "id": "2", "select": "1", "input": "aa" }, 4 { "id": "3", "select": "1", "input": "aa" }, 5 { "id": "4", "select": "1", "input": "bb" }, 6 { "id": "5", "select": "1", "input": "bb" }, 7 { "id": "6", "select": "1", "input": "aa" } 8 ]; 9 10 11 12 var arr2 = arr; //存放 tr 对象 (这两个数组对象用来将来用来筛选) 13 var arrIndex = new Array(); //存放已经有重复对象的索引 14 var result = new Array(); //遍历的结果数组 15 16 function demo(){ 17 //这个辅助方法用来 筛选 18 var digui = function (obj, arr) { 19 20 var ids = obj.id; 21 for (var i = 0; i < arr.length; i++) { 22 var curObj = arr[i]; 23 if (obj.id != curObj.id && obj.select == curObj.select && obj.input == curObj.input) { //判断有重复对象 24 arrIndex.push(i); //记录索引 25 ids += "," + curObj.id; //拼接ID 26 } 27 } 28 29 if (ids.length > 1) { //有重复记录,数组添加到结果中 30 result.push(ids); 31 } 32 } 33 34 35 for (var i = 0; i < arr.length; i++) { //遍历 arr 中所对象有的 36 var curObj = arr[i]; 37 var isExists = false; //标识 38 for (var j = 0; j < arrIndex.length; j++) { 39 if (arrIndex[j] == i) { 40 isExists = true; 41 break; 42 } 43 } 44 45 if (!isExists) { 46 digui(curObj, arr2); 47 } 48 49 } 50 51 52 //遍历 result 输出结果 53 54 for (var m = 0; m < result.length; m++) { 55 var o = result[m]; 56 alert("id "+ o+" 是数据重复") 57 } 58 59 }
<script type="text/javascript"> $(function() { var arr = [ { "id": "1", "select": "1", "input": "aa" }, { "id": "2", "select": "1", "input": "aa" }, { "id": "3", "select": "1", "input": "aa" }, { "id": "4", "select": "1", "input": "bb" }, { "id": "5", "select": "1", "input": "bb" }, { "id": "6", "select": "1", "input": "aa" } ]; var repeatIds = getRepeatArr(arr); for (var i = 0; i < repeatIds.length; i++) { document.write("重复的是id="+ repeatIds[i]+"<br>"); } }); function getRepeatArr(arr) { var result = [], hash = {}; for (var i = 0; i < arr.length ; i++) { var elem = arr[i].select+','+arr[i].input; if (!hash[elem]) hash[elem] = arr[i].id; else hash[elem] = hash[elem] + ',' + arr[i].id; } for (var o in hash) { if (hash[o] && hash[o].split(',').length > 1) { result.push(hash[o]); } } return result; } </script>
你看这样如何...效率应该影响不会很大. 你喜欢就好了
@李丶GuanYao: 这样效率确实高很多,借助 js Object 巧妙的实现,不错, 多谢。
"json".反序列化<集合<实体>>().groupby (m=>new{m.select,m.input}).where(m=>m.count>1).select(m=>m.不太好写,反正就是这个思路.)
谢谢了,这样可以实现