This question already has an answer here:
这个问题已经有了答案:
- Find object by id in an array of JavaScript objects 28 answers
- 在JavaScript对象数组中按id查找对象28个答案
- How to find object in array by property in javascript? 1 answer
- 如何在javascript中按属性找到数组中的对象?1回答
I have following json
我有下面的json
var dictionary = [{"key":"Math","value":"20"},{"key":"History","value":"10"},{"key":"Chemistry","value":"12"}]
I can access for instance the second element of the array like this:
我可以像这样访问数组的第二个元素:
dictionary[1].value
it returns 10 which is the score of the History subject. What I'm looking for is the way so that I can access it by the word "History" itself, I mean I need a code like this:
它返回10,这是历史主题的分数。我想要的是一种方式,这样我就可以用“历史”这个词来访问它,我的意思是我需要这样的代码:
dictionary["History"].value
How can I achieve that?
我怎么能做到呢?
3 个解决方案
#1
4
Ok, so here is a hack. You can use Array
as an Object
and insert any key
you want. You can apply forEach
to it and bind keys
with properties
like below.
这是一个技巧。您可以使用数组作为对象并插入任何您想要的键。您可以对它应用forEach并使用如下所示的属性绑定键。
var dictionary = [{"key":"Math","value":"20"},{"key":"History","value":"10"},{"key":"Chemistry","value":"12"}]
dictionary.forEach(function(item) {
dictionary[item.key] = item;
});
console.log(dictionary["History"].value);
Note: This is just a Hack and will fail in case of duplicate entries.
注意:这只是一个技巧,如果条目重复,将会失败。
Edited
编辑
Solution in case of duplicate keys
解决重复的密钥
var dictionary = [{
"key": "Math",
"value": "20"
}, {
"key": "History",
"value": "10"
}, {
"key": "Chemistry",
"value": "12"
}, {
"key": "Chemistry",
"value": "13"
}]
dictionary.forEach(function(item) {
if (dictionary[item.key] && !Array.isArray(dictionary[item.key])) {
dictionary[item.key] = [dictionary[item.key]];
dictionary[item.key].push(item);
} else if (dictionary[item.key] && Array.isArray(dictionary[item.key])) {
dictionary[item.key].push(item);
} else {
dictionary[item.key] = item;
}
});
console.log(dictionary["Chemistry"]);
#2
1
By using find()
to iterate over your array.
通过使用find()迭代数组。
From MDN Array.prototype.find():
从MDN Array.prototype.find():
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
find()方法返回数组中满足所提供的测试函数的第一个元素的值。否则将返回未定义。
const dictionary = [{"key":"Math","value":"20"},{"key":"History","value":"10"},{"key":"Chemistry","value":"12"}]
const result = dictionary.find(item => {
// if this returns `true` then the currently
// iterated item is the one found
return item.key === 'History'
})
console.log(result)
There's more than one way to do this but this one is the most straightforward and succinct.
有不止一种方法可以做到这一点,但是这个方法是最直接、最简洁的。
#3
1
Try this:
试试这个:
var dictionary = [
{"key":"Math","value":"20"},
{"key":"History","value":"10"},
{"key":"Chemistry","value":"12"}
];
function getValue(searchKey) {
var retVal;
dictionary.some(item => {
if (item.key === searchKey) {
retVal = item.value;
return true;
}
});
return retVal;
}
console.log(getValue('History'));
If goes through your array of objects and finds the object that matches its key
to your searchKey
and returns the result.
如果遍历对象数组,并找到与搜索键匹配的对象并返回结果。
Or you can convert your array of objects into a single object and then reference it directly:
或者你可以将你的对象数组转换成一个对象,然后直接引用它:
var dictionary = {};
[
{"key":"Math","value":"20"},
{"key":"History","value":"10"},
{"key":"Chemistry","value":"12"}
].forEach(item => {dictionary[item.key] = item.value;});
console.log(dictionary.History);
#1
4
Ok, so here is a hack. You can use Array
as an Object
and insert any key
you want. You can apply forEach
to it and bind keys
with properties
like below.
这是一个技巧。您可以使用数组作为对象并插入任何您想要的键。您可以对它应用forEach并使用如下所示的属性绑定键。
var dictionary = [{"key":"Math","value":"20"},{"key":"History","value":"10"},{"key":"Chemistry","value":"12"}]
dictionary.forEach(function(item) {
dictionary[item.key] = item;
});
console.log(dictionary["History"].value);
Note: This is just a Hack and will fail in case of duplicate entries.
注意:这只是一个技巧,如果条目重复,将会失败。
Edited
编辑
Solution in case of duplicate keys
解决重复的密钥
var dictionary = [{
"key": "Math",
"value": "20"
}, {
"key": "History",
"value": "10"
}, {
"key": "Chemistry",
"value": "12"
}, {
"key": "Chemistry",
"value": "13"
}]
dictionary.forEach(function(item) {
if (dictionary[item.key] && !Array.isArray(dictionary[item.key])) {
dictionary[item.key] = [dictionary[item.key]];
dictionary[item.key].push(item);
} else if (dictionary[item.key] && Array.isArray(dictionary[item.key])) {
dictionary[item.key].push(item);
} else {
dictionary[item.key] = item;
}
});
console.log(dictionary["Chemistry"]);
#2
1
By using find()
to iterate over your array.
通过使用find()迭代数组。
From MDN Array.prototype.find():
从MDN Array.prototype.find():
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
find()方法返回数组中满足所提供的测试函数的第一个元素的值。否则将返回未定义。
const dictionary = [{"key":"Math","value":"20"},{"key":"History","value":"10"},{"key":"Chemistry","value":"12"}]
const result = dictionary.find(item => {
// if this returns `true` then the currently
// iterated item is the one found
return item.key === 'History'
})
console.log(result)
There's more than one way to do this but this one is the most straightforward and succinct.
有不止一种方法可以做到这一点,但是这个方法是最直接、最简洁的。
#3
1
Try this:
试试这个:
var dictionary = [
{"key":"Math","value":"20"},
{"key":"History","value":"10"},
{"key":"Chemistry","value":"12"}
];
function getValue(searchKey) {
var retVal;
dictionary.some(item => {
if (item.key === searchKey) {
retVal = item.value;
return true;
}
});
return retVal;
}
console.log(getValue('History'));
If goes through your array of objects and finds the object that matches its key
to your searchKey
and returns the result.
如果遍历对象数组,并找到与搜索键匹配的对象并返回结果。
Or you can convert your array of objects into a single object and then reference it directly:
或者你可以将你的对象数组转换成一个对象,然后直接引用它:
var dictionary = {};
[
{"key":"Math","value":"20"},
{"key":"History","value":"10"},
{"key":"Chemistry","value":"12"}
].forEach(item => {dictionary[item.key] = item.value;});
console.log(dictionary.History);