1

I am trying to read tags from a selected collection of bibliographic data in ZOTERO with Javascript.

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.

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; before the for loop, I get 4943 key:value pairs, which correctly mirrors the number of items in my collection.

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.

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.

However, the above for/of loop works at least for the first item (item "0") and returns the following data:

{
    "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?
  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. Would that be specific to Zotero or Javascript in general?
7
  • 2
    Don't put return inside the loop. It will exit the function on the first iteration. Commented May 12, 2021 at 16:03
  • if itemIDs is an array just map through it Commented May 12, 2021 at 16:08
  • I have moved both return statements out of the loop now. ´´´var s = new Zotero.Search(); s.libraryID = ZoteroPane.getSelectedLibraryID(); var itemIDs = await s.search(); for (itemID of itemIDs) { item = Zotero.Items.get(itemID); itemTAG = item.getTags(); } return itemTAG; return item;´´´ Result is tags of one single item. Will try and fix the for/in loop. Commented May 12, 2021 at 16:09
  • If the code in second block is verbatim, then it appears to be syntactically corrupt. [ ] is an array. Each key/value pair has a : which is indicative of being inside an object literal { }. Moreover, each key/value pair should be delimited with a , whether they are wrapped in { } or not. At best it appears to be one badly formatted string within an array. Commented May 12, 2021 at 16:15
  • Yes, the second block was verbatim, I just took out a few hundred pairs between the squared brackets. It did look odd to me, too, but I am not very familiar with data structures in JS. Interestingly, though, the map() function suggested by @Barmar below worked. I get tags only for all items now, but still wrapped in some kind of array. Commented May 12, 2021 at 16:20

1 Answer 1

2

Use map() to call a function on every array element and return an array of all the results.

return itemIDs.map(itemID => Zotero.Items.get(itemID).getTags())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the results look much better now: [ "0": [ "0": { "tag": "Europa" } ] "1": [ "0": { "tag": "Europa" } ] "2": [ "0": { "tag": "Europa" } ] "3": [ "0": { "tag": "Russia" } "1": { "tag": "Europa" } ] ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.