简体   繁体   English

比较hashMap数组值和Javascript

[英]Compare hashMap arrays values with Javascript

How to compare between two Hashmaps that contains arrays in their values with javascript? 如何使用JavaScript在两个Hashmaps中比较它们的值中包含数组的Hashmap?

Example : Let suppose this is my 2 hashmaps 示例:假设这是我的2个哈希图

1st => 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]

Result => 结果=>

1235 : [1 , 4 , 5]
1237 : [0.33 , 1.51 , 5]

(the goal is to compare the first with the second and display what keys and values of the first hashmap that aren't coherent). (目标是将第一个哈希表与第二个哈希表进行比较,并显示第一个哈希表的哪些键和值不一致)。

I did this at the beginning when my values was String and not arrays : 我一开始是在我的值是String而不是arrays时这样做的:

compareHashMap: function(iAdaptation, iActual){
                var difference = {};
                Object.keys(iActual).forEach(function(k){
                    if(iAdaptation[k] !== iActual[k]){
                        difference[k] = iActual[k];
                    }
                });
                return difference;
            },

PS : iAdaptation and iActual are hashmaps PS:iAdaptation和iActual是哈希图

You can use Array.prototype.filter() and Array.prototype.map() : 您可以使用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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM