关于在这个网站上比较两组JavaScript对象有很多问题,但我无法找到我想要实现的目标。
我有两组对象如下。
var players = [
{ "id" : "45783",
"r3" : "DED" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDDC" },
{ "id" : "78903",
"r3" : "DEDCDBDDDDDC"}
]
var oldPlayers = [
{ "id" : "45783",
"r3" : "DE" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDD" },
{ "id" : "78903",
"r3" : "DEDCDBDDDDD"}
]
我想比较上面的数据并找到var播放器中的确切位置," r3"与oldPlayers不同的价值。
例如,
players ---- "r3" : "DED"
oldPlayers - "r3" : "DE"
上述数据的差异是玩家在[2]的字符位置有额外的D,假设数字从0开始。
我有每个循环,我打算获得这个位置,我被困在如何使这项工作。
var isOldPlayers = false;
// check if oldPlayers exists
if(oldPlayers != undefined && oldPlayers != ''){
isOldPlayers = true;
}
$.each(players,function(i,v){
if(isOldPlayers){
var oldScores = oldPlayers[i]['r3'];
var newScores = this['r3'];
if(newScores != oldScores){
// how can I get the exact location of the difference?
}
}
});
答案 0 :(得分:2)
它可以加速使用数组。如果字符串将包含与上面相同的字符,这将是一个很好的方法。这会对你有用吗?
var dIdx = [v['r3'], oldPlayers[i]['r3']].sort()[0].length;
// ["DED", "DE"].sort() --> ["DE", "DED"];
// ["DE", "DED"][0] --> "DE"
// "DE".length --> 2
// "DED" differs from "DE" at index 2
或者如果你自己需要差异,你可以这样做:
var scores = [v['r3'], oldPlayers[i]['r3']].sort(),
diff = scores[1].substr(scores[0].length);
要清楚,您不需要制作“players
”对象数组。
点击图片进行演示。
var players = [
{ "id" : "45783",
"r3" : "DED" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDDC" },
{ "id" : "78903",
"r3" : "DEDCDBDDDDDC"}
],
oldPlayers = [
{ "id" : "45783",
"r3" : "DE" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDD" },
{ "id" : "78903",
"r3" : "DEDCDBDDDDD"}
];
var desc = '';
$.each(players, function(i, v) {
var dIdx = [v['r3'], oldPlayers[i]['r3']].sort()[0].length;
desc += '<span class="h">' + v['r3'] + '</span> ' +
'and <span class="h">' + oldPlayers[i]['r3'] + '</span> ' +
'differ at index: <span class="hg">' + dIdx + '</span><br><br>';
});
$('body').html(desc);
答案 1 :(得分:1)
如果我理解正确,这应该做你需要的:
var players = [
{ "id" : "45783",
"r3" : "DED" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDDCDEWE" }, // I added chars here to show how to get the rest of the string that is different
{ "id" : "78903",
"r3" : "DEDCDBDDDDDC"}
]
var oldPlayers = [
{ "id" : "45783",
"r3" : "DE" },
{ "id" : "54762",
"r3" : "DEDCCCCDDDD" },
{ "id" : "78903",
"r3" : "DEDCDBDDDDD"}
]
$.each(players,function(index, element){
var newR3= players[index]["r3"].split('');
var oldR3= oldPlayers[index]["r3"].split('');
var curId= players[index]["id"];
$.each(newR3,function(i, el){
if(el != oldR3[i]){
$('#myDiv').append('Object '+index+' (id '+curId+') in players is different than object '+index+' (id '+curId+') in oldPlayers starting at index: '+i +'<br>');
$('#myDiv').append('The rest of the string, from the fist difference to the end is: ' + newR3.slice(i).join('') +'<br><br>');
return false;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv"></div>
答案 2 :(得分:1)
这是一个解决方案:
$.each(players,function(iNew,vNew){
if(isOldPlayers){
$.each(oldPlayers,function(iOld,vOld){
if(vNew.id === vOld.id && vNew.r3 !== vOld.r3){
console.log('players ----- "r3" : ' + vNew.r3 + '\noldPlayers ----- "r3" : ' + vOld.r3);
}
});
}
});
请注意,您需要两个内部循环,外部循环遍及玩家,对于每个循环,内部循环遍历oldPlayers数组以查找匹配。
在内部循环中,您可以使用this.id
和this.r3
代替vOld.id
和vOld.r3
。但是,此代码效率不高,因为必须为第一个数组的每个元素迭代第二个数组的所有元素。如果您知道所有ID都是唯一的,那么您可以使用传统的for
循环进一步改进它,并在找到ID匹配后立即将其中断:
$.each(players,function(iNew,vNew){
if(isOldPlayers){
for (var i = 0, len = oldPlayers.length ; i < len ; i += 1) {
if(vNew.id === oldPlayers[i].id) {
if(vNew.r3 !== oldPlayers[i].r3){
console.log('players ----- "r3" : ' + vNew.r3 + '\noldPlayers ----- "r3" : ' + oldPlayers[i].r3);
}
break;
}
}
}
});