How to iterate over account history items

in javascript •  7 years ago  (edited)

Using steem.js, it is easy to iterate over all the history items of an account. However, if you have not done this before, it may be a little tricky, because there is not enough documentation on the web (or maybe I didn’t find it).

Anyway, steem.api.getAccountHistory returns a promise that resolves to a batch of history items. Each item has an ID number (that starts with 1 for the first item in the history). The batch returned is sorted ascendingly, and contains limit + 1 items, starting at from. If you want to start at the most recent item, you can give from a value of -1. Otherwise, from must be larger than or equal to limit.

Here is an example code for getting history items:

    for (let from = -1, limit = 100, going = true; !!from && going; ) {
        let items = await steem.api.getAccountHistory(username, from, Math.min(limit, from));
        from = items[0][0] - 1;
        items.reverse();
        loop:
        for (let item of items) {
            if (someTest(item)) {
                going = false;
                break loop;
            }
            // do something with item
        }
    }

The loop ends if we reach the first item (hence the use of !!from) or if someTest(item) returns true.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

It's totally a new thing for me. Thanks for sharing this important information.

Loading...

I didn't know about it. But i am interested to know about it.

nice ♥

your are really good at programming, i also like progaramming , l like c language

Good writing.Its help us .

Sir! Is your account hacked? 3 consecutive posts other than idioms? Never seen it before~!

I am going to post more content like this. I am currently doing a programming project and I will be posting some of the tips that I think may be useful.

I do have an interest in computer programming but it takes too much time to get used to it. But after all its worth to give time to learn it.

1117.jpg

Helpful post...

You are doing a lot of good work. It looks very good to me because of your work. You are teaching us Persian language very much.

Wonderful and informative technology. Excellent post.

Really great post

very interesting good post @ghasemkiani

Wow...Amazing post..I appreciate technology..I like javascript.thanks for share...

very nice technology my dear friend @ghasemkiani
Thanks for this post,,

Thanks for this programming tutorial.Javascript is very useful for all browser,and huge app.
Carry on you activity

Its very good lesson for us and this post very useful.
Thabks sir @ghasemkiani for sharing information post.

Stay blessed

nice post friends with beautiful article.

Helpful post thanks for information @ghasemkiani

Thanks for your educative post

I need to know more about it, this is era of technology and we need to be informed. Please I need more tutorial from you thanks

good post

hi my dear friend @ghasemkiani.
Now I began to think hard about posting idioms.
my post is not desirable as posting idioms you my friend @ghasemkiani.
I hope your help for idiom posting tricks in order to follow your footsteps.
Thanks so much :)

Its very interesting and informative blog. Its very nice steeming
All the best

nice post dear @ghasemkiani. i miss you all time your post

Very useful tutorial on javascript.

That a great post. its a helpfull for us.

Thats a great sir..I appreciate ...

its pretty helpful. btw jquery could be more shorter... anyway great work

Helpful for us.

i like this technology thanks for sharing i within for you next post best of luck..

Nice post. i like this technology thanks for sharing. @ghasemkiani

wow , you create a new kind of post.

nice

Hallo my friend @ghamsekiani
Your posts are very good
I like it ...

If you do not mind upvote my post too: https://www.steemit.com/sports/@mahyul/join-madrid-neymar-ready-to-be-paid-cheap

nice....helpfull post...thanks

You got a 0.78% upvote from @postpromoter courtesy of @ghasemkiani! Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!