2017-06-29 65 views
-1

我有一個問題,關於我在JS中的哈希映射。我有一些屬性「鍵」重複多次,但至少在我的哈希映射中,我只找到最後一個。如何在hashmap javascript中顯示重複鍵?

例如:讓我們假設有鑰匙(12345, 12346, 12346, 12346, 12347)列表,然後爲每一個我的屬性的一些值

12345 <= 1 
12346 <= 2 
12346 <= 1 
12346 <= 3 
12347 <= 4 

在它顯示最後:12345 : 1, 12346:3 , 12347:4。但我也需要在我的HashMap中的這些信息12346 : 1, 12346 : 2。如何解決它? 這裏是我的代碼:

getFacing: function(iSceneGraph){ 
       var facingArray = {}; 
       if (iSceneGraph.children.length >0){ 
        for (var i = 0; i < iSceneGraph.children.length; i++){ 
         facingArray = Object.assign(facingArray, this.getFacing(iSceneGraph.children[i])); 
        } 
       } 
       if (iSceneGraph.merch.type === "Facing"){ 
        facingArray[iSceneGraph.merch.ean] = iSceneGraph.merch.hcount; 
       } 
       return facingArray; 
      } 
+0

你想實現什麼作爲地圖的關鍵是獨一無二的,重複是不可能是不可能的。你可以像'12346:[2,1,3]' – nikhil

+4

這樣的數組獲得值:你不能在HashMap中有重複的鍵,你應該重新考慮你的問題 – dloeda

+0

不允許在HashMap中。你必須使用另一種結構。可能是包含鍵值對的數組:'[{「12345」:1},{「12346」:2},...]' – marwils

回答

1
**Try this i think this will help you** 
<script type="text/javascript"> 
var animals = ['Cow', 'Cow', 'Dog', 'Cat','Cow']; 
var sounds = ['Moo', 'Oink', 'Woof', 'Miao','Moo']; 
var arr = []; 

for(i = 0; i < animals.length; i++) { 
    var hash = {}; 
    hash[animals[i]] = sounds[i]; 
    arr.push(hash); 
} 

console.log(arr); 

</script>