Javascript - 通过键获取特定 JSON 数组元素中的属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5901043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:44:47  来源:igfitidea点击:

Javascript - get the value of a property within a specific JSON array element by its key

javascriptarraysjsonkey

提问by gladtobegrey

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().

...我将其加载到我的 javascript 应用程序中以通过 JSON.parse() 成为一个对象。

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?

我用谷歌搜索了很多网站,但收效甚微。或者我会更好地简化/删除数组以支持简单的对象列表?

Thanks.

谢谢。

采纳答案by Amjad Masad

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 结构,您应该将其实现为一个对象,而不是一个对象数组,其中键是 key2 的值​​,值是对象。

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"]

回答by Rudie

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:

没有更优雅的方法可以做到这一点。没有循环你唯一知道的是索引。这不是您想要的标识符,因此您必须检查内容:循环:

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.

或类似的东西。

回答by gladtobegrey

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”的元素 - 大部分时间通过索引访问数组,只有偶尔通过循环搜索,所以这似乎是最好的平衡。我也意识到我可以去掉包含对象“地图”,因为它没有添加任何实用程序。