javascript - Delete element of an array -
before randomly voting down question , suggesting use splice maybe read i'm trying do. thank you.
i'm writing node.js chat bot , 1 of commands need delete specific element of array.
this how command supposed work:
the items 'foo','bar','baz' added array called raidloot. each of these array elements array created users can add , each of array random user selected. if have raidloot = [foo] , second array called foo = [usera,userb,userc], after 1 of users selected 'foo' should removed raidloot.
this relevant code
case 'loot': query(connection, 'select * channel name = "' + username + '"').done(function (result) { if (result[0].length !== 0) { if (args[1] === 'clear') { raidloot = [] send_privgrp_message(botid, username + ' cleared loot list') } else { raidloot.push(args.slice(1).join(' ')) send_privgrp_message(botid, username + ' added ' + args.slice(1).join(' ') + ' slot #' + raidloot.length + '. use !add ' + raidloot.length + ' join ') lootslot[raidloot.length] = [] } } else { send_message_private(userid, 'you have join channel first') } }) break;
and problem is:
exports.flatroll = flatroll = function (userid, args) { if (raidloot.length === 0) { send_message_private(userid, 'there no loot') return } connectdb().done(function (connection) { checkaccess(userid).done(function (result) { userac = result access_req(connection, 'rem').done(function (result) { if (result[0].length === 0 || result[0].length > 0 && result[0][0].status === 'enabled') { if (result[0].length === 0 || result[0][0].access_req <= userac) { getusername(connection, userid).done(function (result) { username = result[0][0].name if (!args) { winnerlist = '<center> <font color=#ffff00> :::flatroll results::: </font> </center> \n' (loot in raidloot) { winnerlist += '<font color=#00ffff>slot #' + (+loot + 1) + '</font> \n' winnerlist += 'item: ' + raidloot[loot] + '\n' if (lootslot[+loot + 1].length === 0) { winnerlist += 'winner: no 1 added \n' } else { winnerlist += 'winner:</font><font color=#00ff00>' + _.sample(lootslot[+loot + 1]) + '</font> \n' lootslot[+loot + 1] = [] raidloot.splice(loot, 1) // line need better alternative for. } winnerlist += '<img src=tdb://id:gfx_gui_friendlist_splitter>\n' } send_privgrp_message(botid, blob('winner list', winnerlist)) connection.release() } // else args }) } else { connection.release() send_message_private(userid, 'access denied') } } else { connection.release() send_message_private(userid, 'command disabled') } }) }) }) }
'raidloot.splice(loot, 1)' work how it's supposed result not need, if there multiple items added , users join 1 of them displayed list contain items except last one, length of raidloot modified loop doesnt last item , on multiple items, less displayed.
i'm new @ node, doing project way learn don't harsh on newb code :)
seems works if split loop in 2 separate loops, 1 extracting winner , second removing array winner found.
if (!args) { winnerlist = '<center> <font color=#ffff00> :::flatroll results::: </font> </center> \n' var rl = raidloot (i = 0; < rl.length; i++) { winnerlist += '<font color=#00ffff>slot #' + (i + 1) + '</font> \n' winnerlist += 'item: ' + raidloot[i] + '\n' if (lootslot[i + 1].length === 0) { winnerlist += 'winner: no 1 added \n' } else { shuffleusers = _.shuffle(lootslot[i + 1]) console.log(shuffleusers) winnerlist += 'winner:</font><font color=#00ff00>' + _.sample(shuffleusers) + '</font> \n' } winnerlist += '<img src=tdb://id:gfx_gui_friendlist_splitter>\n' } send_privgrp_message(botid, blob('winner list', winnerlist)) (i = 0; < rl.length; i++) { if (lootslot[i + 1].length > 0) { lootslot[i + 1] = [] raidloot = _.without(raidloot, rl[i]) } } connection.release() }
Comments
Post a Comment