Eden1998
Eden1998

Reputation: 139

how to only return the last element of an array and not get all elements

Here in the typescript function I receive for downloads all items, but I only want to return the last element of the lists. But I don't know how to customize the function.

Here is my code. blostream is the array [], but i only want to return i=5 for example, if 5 is the last item. How can I fix it?

async function main() {
    let i = 1;

    for await (const blob of containerClient.listBlobsFlat()) {
        const blockBlobClient = containerClient.getBlockBlobClient(blob.name);

        const downloadBlockBlobResponse = await blockBlobClient.download(0);
        const download = await blobToString(await downloadBlockBlobResponse.blobBody);
        console.log(download);
        blobstream.push(download);
    }
    return blobstream;
}

Upvotes: 0

Views: 1247

Answers (3)

Yashar Azadvatan
Yashar Azadvatan

Reputation: 159

With this line of code you can return the last item:

let lastItem= blobstream[blobstream.length - 1];
return lastItem;

good luck!

Upvotes: 1

Philipp Kief
Philipp Kief

Reputation: 8613

You have to return the last item of blobstream like this:

return blobstream[blobstream.length - 1];

Upvotes: 2

I think maybe it will work for you

let result = await containerClient.listBlobsFlat()
let blob = result[result.length - 1]
...

hope this can help you

Upvotes: 1

Related Questions