2015-05-29 72 views
1

我想了解JavaScript中KeyValue對列表的概念。如何從鍵值對列表中獲取值?

我必須使用數組嗎?我想這個數據添加到一個數組:

key: 1, value: "apple" 
key: 2, value: "banana" 
key: 7, value: "cherry" 
key: 24, value: "grapes" 

但我如何獲得關鍵的7不使用循環的價值?

我用一個例子來創建JSFiddle。我如何編輯這個例子來返回密鑰的值?此時它返回第一個輸入的keyValue對的值。

JSFiddle

回答

4

不要讓你的「字典」鍵/值對的列表。相反,使用對象,讓你自動所有功能:

var dict = {}; // object literal 

function addToList() { 
    var aKey = document.getElementById("fkey").value; 
    var aValue = document.getElementById("fvalue").value; 
    dict[aKey] = aValue; 
} 

function getFromList() { 
    var aKey = document.getElementById("rkey").value; 
    document.getElementById("rvalue").value = dict[aKey]; 
} 
+0

完美,正是我一直在尋找。不知道[]和{}之間的區別 – Whistler

0

在你的榜樣,你是不是使用與鍵,值一個JSON對象,這意味着你不能從一鍵搞定的價值你dict數組是這樣的:

dict.value; 

你需要尋找特定關鍵字的索引來獲取值,就像這樣:

function getValue (key) { 
    for (var index in dict) { 
     if(dict[index].key == key) { 
      return dict[index].value; 
     } 
    } 

    return 'Not Found'; 
} 

檢查jsFiddle

2

您正在使用JSON格式的Javascript對象數組。但在Javascript中使用JSON格式的對象更方便。

var fruits = { 
    "1": "apple", 
    "2": "banana", 
    "7": "cherry" 
    "24": "grapes" 
}; 

所以也更容易訪問值:

fruits["7"] 

另外,你不必來管理自己的密鑰的唯一性。如果你把一個值使用相同的密鑰,舊值將被改寫:

fruits["7"] = "strawberry"; 
0

看看這個plugin/gist from KnightCoder

下面是這個插件的實際源代碼。

/* 
* DictionaryKnight v1.0 - A very simple teeny-tiny dictionary class. 
* * @ Developer : https://github.com/KnightCoder 
* * * You can add an item into the dictionary 
* * * You can even directly add a collection of items 
* * * You can get length of the items in the dictionary 
* * * You can easily parse and get the items in the dictionary 
* * * Very small, fast and efficient 
*/ 

var DictionaryKnight = function() { }; 
DictionaryKnight.prototype = { 
    getLength: function() { return Object.keys(this).length }, 
    add: function (key, value) { 
     this[key] = value; 
     return this; 
    }, 
    addCollection: function (arr) { 
     for (var i = 0; i < arr.length; i++) { 
      this.add(arr[i][0], arr[i][1]); 
     } 
     return this; 
    } 
} 

它有一個很小的字典功能。

您可以使用它像這樣:

var dict = new DictionaryKnight(); 
dict.add("en", { "language": "English", "country": "Great Britain", "sample": "This is english" }); 
dict.add("hi", { "language": "Hindi", "country": "India", "sample": "यह हिन्दी है" }); 

var arr = []; 
arr.push(["es", { "language": "Spanish", "country": "Spain", "sample": "Esto es español" }]); 
arr.push(["cn", { "language": "Chinese", "country": "China", "sample": "這是中國" }]); 
dict.addCollection(arr); 

console.log("Total data : " + dict.getLength()); 
for (var i = 0; i < Object.keys(dict).length; i++) { 
    console.log(Object.keys(dict)[i]); 
    console.log(dict[Object.keys(dict)[i]]); 
    console.log("-----"); 
} 

在第一種方法,你可以插入鍵值格式的數據,並在第二個方法,你可以在2維陣列格式添加數據的集合。 您也可以像上面的示例中所示組合使用這兩種方法。

+0

雖然此鏈接可能會回答問題,但最好在此處包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – lin

+0

根據您的建議完成 –

1

您可以瞭解更多關於JavaScript的對象從下面的鏈接:

JavaScript-objects-in-detail

MDN JavaScript Object

如:

var fruits = { 
    "1": "apple", 
    "2": "banana", 
    "7": "cherry", 
    "24": "grapes", 
}; 

要訪問任意鍵如:fruits[1],輸出:apple

var fruits = { 
     "apple": "1", 
     "banana": "2", 
     "cherry": "7", 
     "grapes": "24", 
    }; 

訪問任意鍵例如爲:fruits.apple,輸出:1