Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Javascript从字典中读取值_Javascript_Loops - Fatal编程技术网

用Javascript从字典中读取值

用Javascript从字典中读取值,javascript,loops,Javascript,Loops,我试图用Javascript从ZOTERO中选定的书目数据集合中读取标记 对于那些不熟悉ZOTERO的人:它有一个内置的“RunJS”面板,可以直接处理独立版本中选择/标记的项目 这是我用来从选定文件夹读取数据和访问标记的脚本: var s = new Zotero.Search(); s.libraryID = ZoteroPane.getSelectedLibraryID(); var itemIDs = await s.search(); for (itemID of itemIDs)

我试图用Javascript从ZOTERO中选定的书目数据集合中读取标记

对于那些不熟悉ZOTERO的人:它有一个内置的“RunJS”面板,可以直接处理独立版本中选择/标记的项目

这是我用来从选定文件夹读取数据和访问标记的脚本:

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;
    }
当我调用
返回itemIDs时
for
循环之前,我得到4943对
key:value
对,它正确地反映了我收藏中的项目数量

结构如下所示:

[
    "0": 21848
    "1": 21849
    "2": 21850
    "3": 21851
    "4": 21852
    "5": 21853
    "6": 21854
    "7": 21855
    "8": 21856
    "9": 21857
    "10": 21858
]
我实际上想做的是遍历所有ID,以获取每个项目的书目数据并返回标记

这就是为什么我第一次尝试使用for/in循环,但没有成功,可能是因为我没有正确调用
键:值
对(对应于Python中的字典?)

但是,上述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"
}
所以我的两个问题是:

  • 如何创建一个适当的for/in循环来检索每个项目的相同数据
  • 如何仅返回标签?看起来,
    item.getTags()
    [我在文档中使用的与
    getNotes()
    示例类似的方法]可能不是有效的函数。这是针对Zotero还是Javascript的
  • 使用
    map()
    对每个数组元素调用函数并返回所有结果的数组

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

    不要将
    return
    放入循环中。它将在第一次迭代时退出函数。如果
    itemIDs
    是通过itI映射的数组,则我现在已将两个返回语句移出循环。``var s=new Zotero.Search();s、 libraryID=ZoteroPane.getSelectedLibraryID();var itemIDs=wait s.search();对于(itemID of itemIDs){item=Zotero.Items.get(itemID);itemTAG=item.getTags();}返回itemTAG;退货项目;结果是一个项目的标签。将尝试修复for/in循环。如果第二个块中的代码是逐字的,则它似乎在语法上已损坏<代码>[]是一个数组。每个键/值对都有一个
    ,表示位于对象文本
    {}
    中。此外,每个键/值对都应该用
    分隔,无论它们是否包装在
    {}
    中。充其量只是数组中的一个格式错误的字符串。是的,第二个块是逐字逐句的,我只是在方括号之间取出了几百对。这对我来说也很奇怪,但我对JS中的数据结构不是很熟悉。有趣的是,下面@Barmar建议的
    map()
    函数起了作用。我现在只得到所有项目的标记,但仍然包装在某种数组中。谢谢,现在结果看起来好多了:
    [“0”:[“0”:{“tag”:“Europa”}]“1”:[“0”:{“tag”:“Europa”}]“2”:[“0”:{“tag”:“Europa”}]“3”:[“0”:{“标签”:“俄罗斯”}“1”:{“标签”:“欧罗巴”}]