coffeescript循环遍历数组并添加值

时间:2012-03-26 17:33:18

标签: arrays loops coffeescript iteration

我想做的是为每个经理(在一个数组中)添加一组学生。

这是我陷入困境的地方:

      for sup in sups
        do(sup) ->
          sup.students_a = "This one works"
          getStudents sup.CLKEY, (studs) ->
            sup.students_b = "This one doesn't"
      cback sups
编辑:经过一番思考后,可能会发生的事情是它将“sudents_b”数据添加到sups数组,但是在执行此工作之前,正在返回sups数组(通过cback函数)。因此,我想我应该将该工作转移到一个函数,并且只在执行另一个回调后返回sups?

对于上下文,这是此代码的要点:

odbc = require "odbc"

module.exports.run = (managerId, cback) ->
  db2 = new odbc.Database()
  conn = "dsn=mydsn;uid=myuid;pwd=mypwd;database=mydb"
  db2.open conn, (err) ->
    throw err if err

    sortBy = (key, a, b, r) ->
      r = if r then 1 else -1
      return -1*r if a[key] > b[key]
      return +1*r if b[key] > a[key]
      return 0

    getDB2Rows = (sql, params, cb) ->
      db2.query sql, params, (err, rows, def) ->
        if err? then console.log err else cb rows

    getManagers = (mid, callback) ->
      supers = []
      queue = []

      querySupers = (id, cb) ->
        sql = "select distinct mycolumns where users.id = ? and users.issupervisor = 1"
        getDB2Rows sql, [id], (rows) ->
          for row in rows
            do(row) ->
              if supers.indexOf row is -1 then supers.push row
              if queue.indexOf row is -1 then queue.push row
          cb null

      addSupers = (id) -> # todo: add limit to protect against infinate loop
        querySupers id, (done) ->
          shiftrow = queue.shift()
          if shiftrow? and shiftrow['CLKEY']? then addSupers shiftrow['CLKEY'] else
            callback supers

      addMain = (id) ->
        sql = "select mycolumns where users.id = ? and users.issupervisor = 1"
        getDB2Rows sql, [id], (rows) ->
          supers.push row for row in rows

      addMain mid
      addSupers mid

    getStudents = (sid, callb) ->
      students = []

      sql = "select mycols from mytables where myconditions and users.supervisor = ?"
      getDB2Rows sql, [sid], (datas) ->
        students.push data for data in datas
        callb students

    console.log "Compiling Array of all Managers tied to ID #{managerId}..."
    getManagers managerId, (sups) ->
      console.log "Built array of #{sups.length} managers"
      sups.sort (a,b) ->
        sortBy('MLNAME', a, b) or # manager's manager
        sortBy('LNAME', a, b) # manager
      for sup in sups
        do(sup) ->
          sup.students_a = "This one works"
          getStudents sup.CLKEY, (studs) ->
            sup.students_b = "This one doesn't"
      cback sups

1 个答案:

答案 0 :(得分:1)

你的回调cback subs在第一个getStudents使用studs数组执行回调之前执行是正确的。由于你想为一个完整的数组做这个,所以只用一个for循环它就会变得有点毛茸茸。

我总是建议async来做这些事情:

getter = (sup, callback) ->
  getStudents sup.CLKEY, callback

async.map sups, getter, (err, results) ->
  // results is an array of results for each sup
  callback() // <-- this is where you do your final callback.

修改或者如果您想在每个students上加sup,您就会拥有getter

getter = (sup, callback) ->
  getStudents sup.CLKEY, (studs) ->
    sup.students = studs
    // async expects err as the first parameter to callbacks, as is customary in node
    callback null, sup

编辑:此外,您应该遵循将err作为所有回调的第一个参数传递的节点自定义,并进行正确的错误检查。