如何比较两个包含javascript值数组的Hashmaps?
示例:假设这是我的2个哈希映射
1st =>
1234 : [1.03 , 2.17 , 3]
1235 : [1 , 4 , 5]
1236 : [2 , 3 , 3]
1237 : [0.33 , 1.51 , 5]
2nd =>
1234 : [1.03 , 2.17 , 3]
1235 : [1.17 , 2 , 3.9]
1236 : [2 , 3 , 3]
1237 : [2 , 1 , 5]
结果=>
1235 : [1 , 4 , 5]
1237 : [0.33 , 1.51 , 5]
(目标是比较第一个和第二个,并显示第一个不连贯的hashmap的键和值。)
答案 0 :(得分:0)
我在开头做的时候我的值是String而不是数组:
compareHashMap: function(iAdaptation, iActual){
var difference = {};
Object.keys(iActual).forEach(function(k){
if(iAdaptation[k] !== iActual[k]){
difference[k] = iActual[k];
}
});
return difference;
},
P.S:iAdaptation和iActual是哈希图
答案 1 :(得分:0)
您可以使用Array.prototype.filter()和Array.prototype.map():
const one = {1234: [1.03, 2.17, 3],1235: [1, 4, 5],1236: [2, 3, 3],1237: [0.33, 1.51, 5]};
const two = {1234: [1.03, 2.17, 3],1235: [1.17, 2, 3.9],1236: [2, 3, 3],1237: [2, 1, 5]};
const result = Object.keys(one)
.filter(k => one[k].toString() !== two[k].toString())
.map(k => ({[k]: one[k]}));
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }