如何从键值对列表中获取值?

问题描述:

我想了解JavaScript中KeyValue对列表的概念。如何从键值对列表中获取值?

我必须使用数组吗?我想这个数据添加到一个数组:

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

但我如何获得关键的7不使用循环的价值?

我用一个例子来创建JSFiddle。我如何编辑这个例子来返回密钥的值?此时它返回第一个输入的keyValue对的值。

JSFiddle

不要让你的“字典”键/值对的列表。相反,使用对象,让你自动所有功能:

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

在你的榜样,你是不是使用与键,值一个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

您正在使用JSON格式的Javascript对象数组。但在Javascript中使用JSON格式的对象更方便。

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

所以也更容易访问值:

fruits["7"] 

另外,你不必来管理自己的密钥的唯一性。如果你把一个值使用相同的密钥,旧值将被改写:

fruits["7"] = "strawberry"; 

看看这个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

根据您的建议完成 –

您可以了解更多关于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