我想在JSON中执行合并和比较内部对象。
StudentJson:[
{ name : "harish",
empid: "323234",
exams: "central"
},
{ name : "harish",
empid: "323234",
week41: "30",
week42: "20",
},
{ name : "harish",
empid: "323234",
week47: "47",
week50: "86",
},
{ name : "harish",
empid: "323234",
week51: "67",
week52: "90",
},
{ name : "kishore",
empid: "783433",
exams: "state"
},
{ name : "kishore",
empid: "783433",
week20: "23",
week23: "56",
},
{ name : "kishore",
empid: "323234",
week30: "75",
week38: "73",
},
{ name : "kishore",
empid: "323234",
week40: "23",
week41: "86",
},...
]
预期的Json输出:
StudentJson:[
{ name : "harish",
empid: "323234",
exams: "central",
week41: "30",
week42: "20",
week47: "47",
week50: "86",
week51: "67",
week52: "90"
},
{ name : "kishore",
empid: "783433",
exams: "state",
week20: "23",
week23: "56",
week30: "75",
week38: "73",
week40: "23",
week41: "86"
}
]
请帮我在基本的JavaScript程序中实现。
答案 0 :(得分:0)
您可以执行此功能
var deepEquals = function(o1, o2) {
var k1 = Object.keys(o1).sort();
var k2 = Object.keys(o2).sort();
if (k1.length != k2.length) return false;
return k1.zip(k2, function(keyPair) {
if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){
return deepEquals(o1[keyPair[0]], o2[keyPair[1]])
} else {
return o1[keyPair[0]] == o2[keyPair[1]];
}
}).all();
}
});
想想看,你有了带有8个值的StudentJson,你需要逐个进行比较。
我们可以使用循环:
for(var i=0;i < StudentJson.length; i++){
//Here we do compare the value of the first position with the second position
if (deepEquals(StudentJson[i], StudentJson[i+1])){
//This condition is going returning true or false
}
}
答案 1 :(得分:0)
修改,更新
注意,显示StudentJson
包含语法错误,重复的empid
值?在index
1
,2
,3
,5
,6
,{{1}的对象内的尾随逗号} 7
内。此外,对于具有属性名称StudentJson
的对象(即empid
),最后两个具有属性名称kishore
的对象的empid
值与harish
相同。
调整上述语法错误并重复"323234"
值
JSON
empid
JS
var StudentJson = [
{
"name": "harish",
"empid": "323234",
"exams": "central",
"week41": "30",
"week42": "20",
"week47": "47",
"week50": "86",
"week51": "67",
"week52": "90"
},
{
"name": "harish",
"empid": "323234",
"week41": "30",
"week42": "20"
},
{
"name": "harish",
"empid": "323234",
"week47": "47",
"week50": "86"
},
{
"name": "harish",
"empid": "323234",
"week51": "67",
"week52": "90"
},
{
"name": "kishore",
"empid": "783433",
"exams": "state",
"week20": "23",
"week23": "56",
"week30": "75",
"week38": "73",
"week40": "23",
"week41": "86"
},
{
"name": "kishore",
"empid": "783433",
"week20": "23",
"week23": "56"
},
{
"name": "kishore",
"empid": "783433",
"week30": "75",
"week38": "73"
},
{
"name": "kishore",
"empid": "783433",
"week40": "23",
"week41": "86"
}
];
var res = [];
StudentJson.map(function(v, k) {
if (k === 0) {
res.push(v);
};
if (k > 0 && v.name === res[0].name) {
$.extend(res[0], v)
} else if(v.name !== res[0].name) {
res[1] ? $.extend(res[1], v) : res.push(v)
};
});
&#13;
var StudentJson = [
{
"name": "harish",
"empid": "323234",
"exams": "central",
"week41": "30",
"week42": "20",
"week47": "47",
"week50": "86",
"week51": "67",
"week52": "90"
},
{
"name": "harish",
"empid": "323234",
"week41": "30",
"week42": "20"
},
{
"name": "harish",
"empid": "323234",
"week47": "47",
"week50": "86"
},
{
"name": "harish",
"empid": "323234",
"week51": "67",
"week52": "90"
},
{
"name": "kishore",
"empid": "783433",
"exams": "state",
"week20": "23",
"week23": "56",
"week30": "75",
"week38": "73",
"week40": "23",
"week41": "86"
},
{
"name": "kishore",
"empid": "783433",
"week20": "23",
"week23": "56"
},
{
"name": "kishore",
"empid": "783433",
"week30": "75",
"week38": "73"
},
{
"name": "kishore",
"empid": "783433",
"week40": "23",
"week41": "86"
}
];
var res = [];
StudentJson.map(function(v, k) {
if (k === 0) {
res.push(v);
};
if (k > 0 && v.name === res[0].name) {
$.extend(res[0], v)
} else if(v.name !== res[0].name) {
res[1] ? $.extend(res[1], v) : res.push(v)
};
});
$("pre").text(JSON.stringify(res, null, 4))
&#13;
我将如何为&#34; N&#34;实现这个StudentJson.map函数。学生json对象..- user162366
尝试
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<pre></pre>
var res = [];
StudentJson.map(function (v, k) {
if (k === 0) {
res.push(v);
} else {
var j = res.filter(function (value, key) {
return value.name === v.name
})[0];
if (j) {
$.extend(res[$.inArray(j, res)], v)
} else {
res.push(v)
}
};
return res
});
&#13;
var StudentJson = [{
"name": "harish",
"empid": "323234",
"exams": "central",
"week41": "30",
"week42": "20",
"week47": "47",
"week50": "86",
"week51": "67",
"week52": "90"
}, {
"name": "harish",
"empid": "323234",
"week41": "30",
"week42": "20"
}, {
"name": "harish",
"empid": "323234",
"week47": "47",
"week50": "86"
}, {
"name": "harish",
"empid": "323234",
"week51": "67",
"week52": "90"
}, {
"name": "kishore",
"empid": "783433",
"exams": "state",
"week20": "23",
"week23": "56",
"week30": "75",
"week38": "73",
"week40": "23",
"week41": "86"
}, {
"name": "kishore",
"empid": "783433",
"week20": "23",
"week23": "56"
}, {
"name": "kishore",
"empid": "783433",
"week30": "75",
"week38": "73"
}, {
"name": "kishore",
"empid": "783433",
"week40": "23",
"week41": "86"
}, {
"name": "abc",
"empid": "123456",
"week30": "123",
"week38": "456"
}, {
"name": "abc",
"empid": "123456",
"week40": "789",
"week41": "012"
}, {
"name": "def",
"empid": "654321",
"week30": "012",
"week38": "345"
}, {
"name": "def",
"empid": "654321",
"week40": "678",
"week41": "901"
}];
var res = [];
StudentJson.map(function (v, k) {
if (k === 0) {
res.push(v);
} else {
var j = res.filter(function (value, key) {
return value.name === v.name
})[0];
if (j) {
$.extend(res[$.inArray(j, res)], v)
} else {
res.push(v)
}
};
return res
});
$("pre").text(JSON.stringify(res, null, 4))
&#13;