javascript - A reliable way of sorting an array and passing that information to a view -


i using expressjs render routes , pass on database information.

  • have 2 arrays 1. paintingjobs 2. customerlist .
  • (1) paintingjobs array has foreign key, contains customer id.
  • i want create array of customers have painting jobs , pass array onto view
  • . filter (2)customerlist array down list of customers have painting jobs; use series of nested loops so.....

         for(var i=0; i<paintingjobs.length; i++) {         for(j=0; j<customerlist.length; j++) {             if (customerlist[j].id == paintingjobs[i].customerid) {                 customerrecords.push(customerlist[j]);             }         }      } 

    creating new filtered array of customerrecords, passed view.

    question is: if sort (1)paintingjobs array date prior using filter (1)customerlist, resulting array (customerrecords) sorted date or more reliable method? if not, appending date customerlist , sorting final array next best solution?

    i passing information view , creating unordered list based on number of records.

    thank time , suggestions

  • your double-loop preserves order of paintingjobs in customerrecords, though unless have duplicate customerlist id's, looks inefficient (why no break?).
    means changes order of paintingjobs before double loop reflected in customerrecords. changes after not.

     paintingjobs.sort(however);   for(var i=0; i<paintingjobs.length; i++) {     for(j=0; j<customerlist.length; j++) {         if (customerlist[j].id == paintingjobs[i].customerid) {             customerrecords.push(customerlist[j]);             break; // next paintingjob         }     }  } 

    it looks items in customerlist objects, if make change object in customerlist, change appear in customerrecords after double loop.


    Comments

    Popular posts from this blog

    image - ClassNotFoundException when add a prebuilt apk into system.img in android -

    I need to import mysql 5.1 to 5.5? -

    Java, Hibernate, MySQL - store UTC date-time -