Javascript 如何从键值对列表中获取值?
我试图理解JavaScript中键值对列表的概念 我必须使用数组吗?我尝试将此数据添加到数组中:Javascript 如何从键值对列表中获取值?,javascript,Javascript,我试图理解JavaScript中键值对列表的概念 我必须使用数组吗?我尝试将此数据添加到数组中: key: 1, value: "apple" key: 2, value: "banana" key: 7, value: "cherry" key: 24, value: "grapes" 但是我如何在不使用循环的情况下获得键7的值呢 我用一个例子创建了JSFIDLE。如何编辑此示例以返回键的值?此时,它正在返回第一个输入的键值对的值 不要将“dict”列为键/值对列表。相反,请使用自动提供所有
key: 1, value: "apple"
key: 2, value: "banana"
key: 7, value: "cherry"
key: 24, value: "grapes"
但是我如何在不使用循环的情况下获得键7的值呢
我用一个例子创建了JSFIDLE。如何编辑此示例以返回键的值?此时,它正在返回第一个输入的键值对的值
不要将“dict”列为键/值对列表。相反,请使用自动提供所有功能的对象:
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];
}
不要将“dict”列为键/值对的列表。相反,请使用自动提供所有功能的对象:
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];
}
在您的示例中,您没有使用带有key、value的json对象,这意味着您无法从
dict
数组中获取key的值,如下所示:
dict.value;
您需要查找特定键的索引以获取值,如下所示:
function getValue (key) {
for (var index in dict) {
if(dict[index].key == key ) {
return dict[index].value;
}
}
return 'Not Found';
}
选中在您的示例中,您没有使用带有key、value的json对象,这意味着您无法从
dict
数组中获取key的值,如下所示:
dict.value;
您需要查找特定键的索引以获取值,如下所示:
function getValue (key) {
for (var index in dict) {
if(dict[index].key == key ) {
return dict[index].value;
}
}
return 'Not Found';
}
选中您正在使用JSON格式的Javascript对象数组。但在Javascript中,只使用JSON格式的对象更方便
var fruits = {
"1": "apple",
"2": "banana",
"7": "cherry"
"24": "grapes"
};
因此,访问这些值也更容易:
fruits["7"]
此外,您不必自己管理密钥的唯一性。如果使用相同的键输入值,旧值将被覆盖:
fruits["7"] = "strawberry";
您正在使用JSON格式的Javascript对象数组。但在Javascript中,只使用JSON格式的对象更方便
var fruits = {
"1": "apple",
"2": "banana",
"7": "cherry"
"24": "grapes"
};
因此,访问这些值也更容易:
fruits["7"]
此外,您不必自己管理密钥的唯一性。如果使用相同的键输入值,旧值将被覆盖:
fruits["7"] = "strawberry";
看看这个
下面是这个插件的实际源代码
/*
* 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;
}
}
/*
*DictionaryKnight v1.0-一个非常简单的小字典类。
**@开发者:https://github.com/KnightCoder
***您可以将项目添加到字典中
***您甚至可以直接添加项目集合
***您可以在字典中获取项目的长度
***您可以轻松解析并获取字典中的项目
***体积小、速度快、效率高
*/
var=function(){};
DictionaryKnight.prototype={
getLength:function(){return Object.keys(this.length},
添加:功能(键、值){
此[键]=值;
归还这个;
},
addCollection:函数(arr){
对于(变量i=0;i
它有一个小小的字典功能
您可以这样使用它:
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("-----");
}
var dict=new DictionaryKnight();
dict.add(“en”,“语言”:“英语”,“国家”:“大不列颠”,“样本”:“这是英语”});
dict.add(“hi”,“语言”:“印地语”,“国家”:“印度”,“样本”:”यह हिन्दी है" });
var-arr=[];
arr.push([“es”,“language”:“西班牙语”,“国家”:“西班牙”,“示例”:“Esto es español”);
arr.push([“cn”,“语言”:“中文”,“国家”:“中国”,“样本”:”这是中国" }]);
dict.addCollection(arr);
log(“总数据:+dict.getLength());
for(var i=0;i
在第一种方法中,可以插入键值格式的数据,在第二种方法中,可以添加二维数组格式的数据集合。
您也可以将这两种方法结合使用,如上面的示例所示。查看此示例
下面是这个插件的实际源代码
/*
* 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;
}
}
/*
*DictionaryKnight v1.0-一个非常简单的小字典类。
**@开发者:https://github.com/KnightCoder
***您可以将项目添加到字典中
***您甚至可以直接添加项目集合
***您可以在字典中获取项目的长度
***您可以轻松解析并获取字典中的项目
***体积小、速度快、效率高
*/
var=function(){};
DictionaryKnight.prototype={
getLength:function(){return Object.keys(this.length},
添加:功能(键、值){
此[键]=值;
归还这个;
},
addCollection:函数(arr){
对于(变量i=0;i
它有一个小小的字典功能
您可以这样使用它:
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("-----");
}
var dict=new DictionaryKnight();
dict.add(“en”,“语言”:“英语”,“国家”:“大不列颠”,“样本”:“这是英语”});
dict.add(“hi”,“语言”:“印地语”,“国家”:“印度”,“样本”:”यह हिन्दी है" });
var-arr=[];
arr.push([“es”,“language”:“西班牙语”,“国家”:“西班牙”,“示例”:“Esto es español”);
arr.push([“cn”,“语言”:“中文”,“国家”:“中国”,“样本”:”这是中国" }]);
dict.addCollection(arr);
log(“总数据:+dict.getLength());
for(var i=0;i
在第一种方法中,可以插入键值格式的数据,在第二种方法中,可以添加二维数组格式的数据集合。
您还可以结合使用这两种方法,如上面的示例所示。您可以从以下链接了解有关JavaScript对象的更多信息: 例如: 要访问任何键,例如:
fruits[1]
,请输出:apple
var fruits = {
"apple": "1",
"banana": "2",
"cherry": "7",
"grapes": "24",
};
访问任何密钥,例如: