0

I try to find a solution to compare two hashmaps in javascript but I have a difficulty.

This is my situation, I have a hashmap composed of a key + value in form of an array.

Example : 125 : [1 , 2 , 3].

And then I take this data and I compare it with another hashmap with this kind.

Example :

123 : [[1 , 1 , 1][2 , 8.7 , 10]]
124 : [[0 , 0, 5.4][3 , 4 , 5][7, 9.1 , 6]
125 : [[1 , 2 , 3][0.4 , 4 , 8]]

The second hashmap is similar to the first , but its value could contain an array of N arrays. The goal : To parse the second hashmap with my first hashmap and search if once it finds the value of the first in an array of the second it must return "OK"

For example in this case : it will return "OK" because we can notice that the key 125 and her value in the first hashmap is include in 125 : [[1 , 2 , 3][0.4 , 4 , 8]]

I should not only test on the values ​​but also on the keys

1
  • What did you try ? Including some code might help people to find an adequate solution for you. Commented Jul 6, 2017 at 14:52

1 Answer 1

2

The use of JSON.stringify can simplify your life:

let compareKey = JSON.stringify([1, 2, 3]);

let hash = {
	123 : [[1 , 1 , 1], [2 , 8.7 , 10]],
	124 : [[0 , 0, 5.4], [3 , 4 , 5], [7, 9.1 , 6]],
	125 : [[1 , 2 , 3], [0.4 , 4 , 8]]
};


for(let hashKey in hash) { 

	hash[hashKey].forEach(el => {
		if (JSON.stringify(el) === compareKey)
			console.log(hashKey);
	});

}

Sign up to request clarification or add additional context in comments.

4 Comments

did u try the code? It seems it won't work because i want to compare the key with her value with other and not only the value of the first key
Please write down a sample of the result you're expecting.
my first hashmap is 125 : [1 , 2 , 3] and I compare this hashmap if it's include in the second like I explained in my question, so I have to search if 125 : [1 , 2 , 3] is in 125 : [[1 , 2 , 3], [0.4 , 4 , 8]] then I return "OK" , but I don't know how to parse all the second hashmap because it doesn't only include 1 key + value. Need more details?
Not only test on the values ​​but also on the keys @gio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.