Javascript - 通过键获取特定 JSON 数组元素内的属性值
我有一个像这样的 JSON 结构:
{
map: [
{"key1":"valueA1", "key2":"valueA2", "key3":"valueA3"},
{"key1":"valueB1", "key2":"valueB2", "key3":"valueB3"},
{"key1":"valueC1", "key2":"valueC2", "key3":"valueC3"},
.... etc
]
}
...我将其加载到我的 javascript 应用程序中,通过 JSON.parse() 成为一个对象。
我想从对象数组的元素(其中 key2='valueB2')检索(比如说)key3 的值。
我可以通过循环来做到这一点,但想知道是否有一种更优雅(例如单行和更有效)的方法来执行此操作,而不必知道数组元素的索引号?
我用谷歌搜索了很多网站,但收效甚微。或者我会更好地简化/删除数组以支持简单的对象列表吗?
谢谢。
I have a JSON structure like this:
{
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().
I want to retrieve (say) the value of key3 from the element of the object array where 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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSON 通常会在除数字之外的所有键和值周围使用双引号 "。
但是循环是最有效和最好的选择。有新的函数式数组迭代方法,但仅在新的 JS 引擎和浏览器上可用,可以找到它们此处:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
如果您可以自由更改 JSON 结构,则应该将其实现为对象而不是一个对象数组,其中键是 key2 的值,值是 Object
示例:
对象的检索时间复杂度为 O(1),就像
obj["valueB2"]
一样简单。JSON will usually have double quotations " around all keys and values except for numbers.
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
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.
Example:
The retrieval of the object would be O(1) and as simple as
obj["valueB2"]
没有更优雅的方法可以做到这一点。无需循环,您唯一知道的就是索引。这不是您想要的标识符,因此您必须检查 content:loop:
或类似的内容。
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:
Or something like that.
总结一下:从两个答案中汲取灵感,我采用了这个更简单的 JSON 结构:
使用建议的循环函数来查找“key2:”valueB2”的元素 - 该数组大部分时间都是通过索引访问的,只有偶尔通过循环搜索,所以这似乎是最好的平衡,我还意识到我可以取消包含对象“地图”,因为它没有添加任何实用程序。
To round off: taking ideas from both answers, I adopted this simpler JSON structure:
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.