简体   繁体   中英

How can I properly create an array and use it outside of the scope in this example?

I have written some code using a colleagues package "db". First it gets a list of sessions of a film shoot day, then it lists the captured shots in the shoot day sessions, and then the "getCapture" function gets the shot information for each shot. I am trying to use this getCapture function to read the shots' information(ie name, size, subjects) along with ['Node Information']['NAME'] in order to just get the shot names only. I want to do this with all sessions of the day and all their shots. I've figured out how to do this and return the list of names in the example below, using "console.log(captureInfo._data['Node Information']['NAME'])" ..However I want to be able to use these results in an array outside of this scope. when I try to push these results like in the second example, it prints out several copies of the names over and over again. I want to be able to add the shoot names to an array so the array can be used outside of this whole scope, just not sure how to do so.

Example that works to log and list all the shots just once, but does not put the results into a variable/array:

      var sessionURL = ("X:\\Private_LowPerf\\TESTING_DATA\\Capture day 1.file");

    async function main (){
      var captures = [];
      var captureInfo = [];
      var captureNames = [];

      if (sessionURL){
        var sessions = await _db.listSessions(sessionURL);
        sessions.forEach(async (item) => {
          if(sessions){
            var captures = await _db.listCaptures(item);
            }

            captures.forEach(async (item) => {
              if(sessions){
                var captureInfo = await _db.getCapture(item.path);

                console.log(captureInfo._data['Node Information']['NAME']);

    }
      })

          })
        }
      }
      main();

Example that isn't working because it prints out too many copies of the names due to a scoping issue I assume:

async function main (){
  var captures = [];
  var captureInfo = [];
  var captureNames = [];

  if (sessionURL){
    var sessions = await _db.listSessions(sessionURL);
    sessions.forEach(async (item) => {
      if(sessions){
        var captures = await _db.listCaptures(item);
        }

        captures.forEach(async (item) => {
          if(sessions){
            var captureInfo = await _db.getCapture(item.path);

}
        captureNames.push(captureInfo._data['Node Information']['NAME']);
        console.log(captureNames);

  })

      })
    }
  }
  main();

You need to put the console log outside the loops, otherwise, everytime the loops execute you will print the current items, plus the previous ones.

Array.prototype.forEachAsync = async function(cb){
    for(let x of this){
        await cb(x);
    }
}


async function main() {
    var captures = [];
    var captureInfo = [];
    var captureNames = [];

    if (sessionURL) {
        var sessions = await _db.listSessions(sessionURL);
        await sessions.forEachAsync(async (item) => {
            if (sessions) {
                var captures = await _db.listCaptures(item);
            }

            await captures.forEachAsync(async (item) => {
                if (sessions) {
                    var captureInfo = await _db.getCapture(item.path);
                    captureNames.push(captureInfo._data['Node Information']['NAME']);
                }

            })

        })


        console.log(captureNames);
    }
}
main();

EDIT: as you are dealing with async functions you need to be sure that your loops were executed before calling console.log. This is why we need the forEachAsync

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM