Sending to different accounts using one transaction

i’m trying to send to multiple accounts using one transaction

my code is as follow:–

----------------- start ---------------

                for(key in distributions)
                {
                    var esCoinAmt = distributions[key].EsCoinAmount;
                    var recipientAddr = distributions[key].EssEntity.Address;

                    var transactionHttp = new nem.TransactionHttp();
                    var mosaicHttp = new nem.MosaicHttp();
                    var account = nem.Account.createWithPrivateKey(privateKey);
             
                    Rx.Observable.from([
                        { mosaic: new nem.MosaicId(MyConstants.BescNamespace, MyConstants.EsCoin), quantity: esCoinAmt }
                    ]).flatMap(function (_) { return mosaicHttp.getMosaicTransferableWithAmount(_.mosaic, _.quantity); })
                        .toArray()
                        .map(function (mosaics) { return nem.TransferTransaction.createWithMosaics(nem.TimeWindow.createWithDeadline(), new nem.Address(recipientAddr), mosaics, nem.PlainMessage.create(energysavingJson)); })
                        .map(function (transaction) { return account.signTransaction(transaction); })
                        .flatMap(function (signedTransaction) { return transactionHttp.announceTransaction(signedTransaction); })
                        .subscribe(function (nemAnnounceResult) {
                        console.log(nemAnnounceResult);
                 
                    });

                }

-------------------- end - ------------------

problem is that all transaction goes to the last recipient’s address

any suggestion how to do this ?

All transactions are exactly same or only to same recipient?
Maybe indeed all values in distributions[key].EssEntity.Address are same?

I did a trace. They are different.

i think the reason… it was sending to the last accout is that
the flatmap() is waiting for the observable to return…
hence it binds the last address in the loop

so i modified the code to pass in the recipient address an output from flatmap

     Rx.Observable.from([
                        { mosaic: new nem.MosaicId(MyConstants.BescNamespace, MyConstants.EsCoin), quantity: esCoinAmt, rAddr: recipientAddr }
                    ]).flatMap(function (_) { return [ mosaicHttp.getMosaicTransferableWithAmount(_.mosaic, _.quantity), _.rAddr ]; })
                        .toArray()
                        .map(function (mosaics) { return nem.TransferTransaction.createWithMosaics(nem.TimeWindow.createWithDeadline(), new nem.Address(mosaics[1]), mosaics[0], nem.PlainMessage.create(energysavingJson)); })
                        .map(function (transaction) { return account.signTransaction(transaction); })
                        .flatMap(function (signedTransaction) { return transactionHttp.announceTransaction(signedTransaction); })
                        .subscribe(function (nemAnnounceResult) {
                        console.log(nemAnnounceResult);
                 
                    });

however
i get the error FAILURE_INSUFFICIENT_FEE

You must tell what is mosaic name, quantity and calculated fee to verify this.

just make sure your MyConstants have value inside.

check on the link below, follow the format.