I have a JSON structure like this:
我有一个像这样的JSON结构:
{
map: [
{"key1":"valueA1", "key2":"valueA2", "key3":"valueA3"},
{"key1":"valueB1", "key2":"valueB2", "key3":"valueB3"},
{"key1":"valueC1", "key2":"valueC2", "key3":"valueC3"},
.... etc
]
}
... which I load into my javascript app to become an object via JSON.parse().
...我通过JSON.parse()加载到我的javascript应用程序中成为对象。
I want to retrieve (say) the value of key3 from the element of the object array where key2='valueB2'.
我想从对象数组的元素中检索(比方说)key3的值,其中key2 ='valueB2'。
I can do it by looping through, but wondered if there was a more elegant (e.g. single line and more efficient) way of doing this, without having to know the index number for the array element?
我可以通过循环来完成它,但想知道是否有更优雅(例如单行和更有效)的方式这样做,而不必知道数组元素的索引号?
I've google loads of sites, to little avail. Or would I be better simplifying/removing the array in favour of a simple list of objects?
我已经google加载网站,但收效甚微。或者我会更好地简化/删除数组,而不是简单的对象列表?
Thanks.
3 个解决方案
#1
3
JSON will usually have double quotations " around all keys and values except for numbers.
JSON通常会在所有键和值周围加上双引号,但数字除外。
However loop is the most efficient and best choice you have. There is new functional array iteration methods, but only available on new JS engines and browsers, they can be found here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
然而,循环是您拥有的最有效和最佳选择。有新的函数数组迭代方法,但只能在新的JS引擎和浏览器上使用,它们可以在这里找到:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
If you have the liberty of changing the JSON structure, you should implement it to be an Object instead of an array of objects where the key is the value of key2 and the value is the Object.
如果您可以*更改JSON结构,则应将其实现为Object而不是对象数组,其中键是key2的值,值是Object。
Example:
{
"valueB2": {
"key1": "valueB1",
"key2": "valueB2",
"key3": "valueB3"
}
}
The retrieval of the object would be O(1) and as simple as obj["valueB2"]
对象的检索将是O(1)并且像obj [“valueB2”]一样简单
#2
1
There isn't a more elegant way to do this. The only thing you know without looping are the indexes. That's not the identifier you want, so you'll have to inspect the content: loop:
没有更优雅的方法来做到这一点。没有循环,你唯一知道的就是索引。这不是你想要的标识符,所以你必须检查内容:loop:
function byKey(arr, key) {
for ( var i=0, L=arr.length; i<L; i++ ) {
if ( arr[i].key1 === key ) {
return arr[i];
}
}
}
Or something like that.
或类似的东西。
#3
0
To round off: taking ideas from both answers, I adopted this simpler JSON structure:
四舍五入:从两个答案中获取想法,我采用了这个更简单的JSON结构:
[
{"key1":"valueA1", "key2":"valueA2", "key3":"valueA3"},
{"key1":"valueB1", "key2":"valueB2", "key3":"valueB3"},
{"key1":"valueC1", "key2":"valueC2", "key3":"valueC3"},
.... etc
]
with the suggested loop function to find the element for "key2:"valueB2" - the array is accessed by index for most of the time, and only occasionally by the loop-search, so that seemed the best balance. I also realised I could do away with the containing object "map" as it wasn't adding any utility.
使用建议的循环函数来查找“key2:”valueB2“的元素 - 大多数时间都是通过索引访问数组,只是偶尔通过循环搜索来访问,所以这似乎是最佳平衡。我也意识到我可以取消包含对象“map”,因为它没有添加任何实用程序。
#1
3
JSON will usually have double quotations " around all keys and values except for numbers.
JSON通常会在所有键和值周围加上双引号,但数字除外。
However loop is the most efficient and best choice you have. There is new functional array iteration methods, but only available on new JS engines and browsers, they can be found here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
然而,循环是您拥有的最有效和最佳选择。有新的函数数组迭代方法,但只能在新的JS引擎和浏览器上使用,它们可以在这里找到:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
If you have the liberty of changing the JSON structure, you should implement it to be an Object instead of an array of objects where the key is the value of key2 and the value is the Object.
如果您可以*更改JSON结构,则应将其实现为Object而不是对象数组,其中键是key2的值,值是Object。
Example:
{
"valueB2": {
"key1": "valueB1",
"key2": "valueB2",
"key3": "valueB3"
}
}
The retrieval of the object would be O(1) and as simple as obj["valueB2"]
对象的检索将是O(1)并且像obj [“valueB2”]一样简单
#2
1
There isn't a more elegant way to do this. The only thing you know without looping are the indexes. That's not the identifier you want, so you'll have to inspect the content: loop:
没有更优雅的方法来做到这一点。没有循环,你唯一知道的就是索引。这不是你想要的标识符,所以你必须检查内容:loop:
function byKey(arr, key) {
for ( var i=0, L=arr.length; i<L; i++ ) {
if ( arr[i].key1 === key ) {
return arr[i];
}
}
}
Or something like that.
或类似的东西。
#3
0
To round off: taking ideas from both answers, I adopted this simpler JSON structure:
四舍五入:从两个答案中获取想法,我采用了这个更简单的JSON结构:
[
{"key1":"valueA1", "key2":"valueA2", "key3":"valueA3"},
{"key1":"valueB1", "key2":"valueB2", "key3":"valueB3"},
{"key1":"valueC1", "key2":"valueC2", "key3":"valueC3"},
.... etc
]
with the suggested loop function to find the element for "key2:"valueB2" - the array is accessed by index for most of the time, and only occasionally by the loop-search, so that seemed the best balance. I also realised I could do away with the containing object "map" as it wasn't adding any utility.
使用建议的循环函数来查找“key2:”valueB2“的元素 - 大多数时间都是通过索引访问数组,只是偶尔通过循环搜索来访问,所以这似乎是最佳平衡。我也意识到我可以取消包含对象“map”,因为它没有添加任何实用程序。