简体   繁体   English

从 Javascript 中的字典中读取值

[英]Reading values from dictionary in Javascript

I am trying to read tags from a selected collection of bibliographic data in ZOTERO with Javascript.我正在尝试使用 Javascript 从 ZOTERO 中选定的书目数据集合中读取标签。

For those who aren't familiar with ZOTERO: it has an in-built "run JS" panel to work directly with items selected / marked in the standalone version.对于那些不熟悉 ZOTERO 的人:它有一个内置的“运行 JS”面板,可以直接处理在独立版本中选择/标记的项目。

This is the script I am using to read data from a selected folder and access the tags:这是我用来从选定文件夹读取数据并访问标签的脚本:

var s = new Zotero.Search();
s.libraryID = ZoteroPane.getSelectedLibraryID();

var itemIDs = await s.search();

for (itemID of itemIDs) {
       item = Zotero.Items.get(itemID);
       return item;
       itemTAG = item.getTags();
       return itemTAG;
    }

When I call return itemIDs;当我调用return itemIDs; before the for loop, I get 4943 key:value pairs, which correctly mirrors the number of items in my collection.for循环之前,我得到了 4943 key:value对,它正确地反映了我的集合中的项目数。

The structure looks like this:结构如下所示:

[
    "0": 21848
    "1": 21849
    "2": 21850
    "3": 21851
    "4": 21852
    "5": 21853
    "6": 21854
    "7": 21855
    "8": 21856
    "9": 21857
    "10": 21858
]

What I would actually like to do is iterate through all IDs to get the bibliographic data for each item and return the tags.我真正想做的是遍历所有 ID 以获取每个项目的书目数据并返回标签。

This is why I first tried a for/in loop , but this didn't work, supposedly because I wasn't calling the key:value pairs (corresponding to a dictionary in Python?) correctly.这就是为什么我首先尝试了for/in 循环,但这不起作用,据说是因为我没有正确调用key:value对(对应于 Python 中的字典?)。

However, the above for/of loop works at least for the first item (item "0") and returns the following data:但是,上述for/of循环至少对第一项(项“0”)有效,并返回以下数据:

{
    "key": "BDSIJ5P4",
    "version": 1085,
    "itemType": "book",
    "place": "[Augsburg]",
    "publisher": "[Gabriel Bodenehr]",
    "date": "[circa 1730]",
    "title": "Constantinopel",
    "numPages": "1 Karte",
    "creators": [
        {
            "firstName": "Gabriel",
            "lastName": "Bodenehr",
            "creatorType": "author"
        }
    ],
    "tags": [
        {
            "tag": "Europa"
        }
    ],
    "collections": [
        "DUW2PJDP"
    ],
    "relations": {
        "dc:replaces": [
            "http://zotero.org/groups/2289797/items/ZB5J5VZK"
        ]
    },
    "dateAdded": "2019-02-13T17:27:29Z",
    "dateModified": "2020-03-23T13:13:13Z"
}

So my two questions are:所以我的两个问题是:

  1. How can I create a proper for/in loop that retrieves these same data for each item?如何创建一个适当的 for/in 循环来为每个项目检索这些相同的数据?
  2. How can I return tags only?我怎样才能只返回标签? It seems that item.getTags() [which I used in analogy to the getNotes() examples in the documentation] may not be a valid function.似乎item.getTags() [我使用的类似于文档中的getNotes()示例] 可能不是有效的 function。 Would that be specific to Zotero or Javascript in general?一般而言,这是否特定于 Zotero 或 Javascript?

Use map() to call a function on every array element and return an array of all the results.使用map()在每个数组元素上调用 function 并返回所有结果的数组。

return itemIDs.map(itemID => Zotero.Items.get(itemID).getTags())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM