javascript - Order of Array Elements Causing Error -


i have 2 arrays. elements of each array shown below:

alllabbranches = [{   "labbranchid": "1",   "labname": "wahab labs, islampura branch, lahore",   "labtitle": "wahab labs lahore",   "shortname": "wahb",   "address": "56 islampura, lahore",   "landlineno": "04237940445",   "datecreated": "2016-03-11 11:00:00",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "2016-05-04 00:53:03",   "updatedbyname": "jamshaid sabir",   "updatedbyuserid": "1",   "clientgroups_clientgroupsid": "1",   "startdate": "2016-05-04" }, {   "labbranchid": "2",   "labname": "wahab labs, model town branch, lahore",   "labtitle": "wahab labs lahore",   "shortname": "wahab",   "address": "45 model town, lahore",   "landlineno": "04237945485",   "datecreated": "2016-03-11 11:00:00",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "0000-00-00 00:00:00",   "updatedbyname": "",   "updatedbyuserid": "",   "clientgroups_clientgroupsid": "1",   "startdate": "0000-00-00" }, {   "labbranchid": "3",   "labname": "shahdara more branch",   "labtitle": "wahab labs lahore",   "shortname": "wahab",   "address": "shahdara",   "landlineno": "04237933415",   "datecreated": "2016-03-11 11:48:15",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "0000-00-00 00:00:00",   "updatedbyname": "",   "updatedbyuserid": "",   "clientgroups_clientgroupsid": "1",   "startdate": "0000-00-00" }, {   "labbranchid": "4",   "labname": "new branch",   "labtitle": "wahab labs lahore",   "shortname": "wahb",   "address": "more samanabad, lahore",   "landlineno": "042361561",   "datecreated": "2016-03-11 11:52:06",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "2016-03-14 15:06:44",   "updatedbyname": "jamshaid sabir",   "updatedbyuserid": "1",   "clientgroups_clientgroupsid": "1",   "startdate": "2016-03-14" }, {   "labbranchid": "5",   "labname": "test branch",   "labtitle": "xyz",   "shortname": "sfwe",   "address": "dsfasd",   "landlineno": "sdfasd",   "datecreated": "2016-03-12 00:14:11",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "2016-08-11 12:54:12",   "updatedbyname": "jamshaid sabir",   "updatedbyuserid": "1",   "clientgroups_clientgroupsid": "1",   "startdate": "2016-03-12" }, {   "labbranchid": "6",   "labname": "test branch 2",   "labtitle": "asdfs",   "shortname": "asdfs",   "address": "asdf",   "landlineno": "asdf",   "datecreated": "2016-03-12 12:16:24",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "0000-00-00 00:00:00",   "updatedbyname": "",   "updatedbyuserid": "",   "clientgroups_clientgroupsid": "1",   "startdate": "2016-03-12" }, {   "labbranchid": "7",   "labname": "wahab labs, shahdara branch, lahore",   "labtitle": "shahdara",   "shortname": "shahdara",   "address": "shahdara",   "landlineno": "0423744915",   "datecreated": "2016-08-11 12:56:27",   "createdbyname": "jamshaid sabir",   "createdbyuserid": "1",   "dateupdated": "2016-08-11 12:57:01",   "updatedbyname": "jamshaid sabir",   "updatedbyuserid": "1",   "clientgroups_clientgroupsid": "1",   "startdate": "2016-08-01" }] 

another array elements are:

labsofuser = [{   "userhasbranchid": "53",   "labbranches_labbranchid": "6",   "usersoflabs_userid": "9" }, {   "userhasbranchid": "54",   "labbranches_labbranchid": "1",   "usersoflabs_userid": "9" }, {   "userhasbranchid": "55",   "labbranches_labbranchid": "7",   "usersoflabs_userid": "9" }, {   "userhasbranchid": "56",   "labbranches_labbranchid": "2",   "usersoflabs_userid": "9" }] 

now have select multiple box , show branches selected , not selected. code given below:

function populateselectedlabs() {      $('#labbranchids').empty();     if (alllabbranches.length >= labsofuser.length) {       (var = 0; < alllabbranches.length; i++) {         (var j = 0; j < labsofuser.length; j++) {           if (alllabbranches[i].labbranchid == labsofuser[j].labbranches_labbranchid) {              $("#labbranchids").append("<option selected='selected' value='" + alllabbranches[i].labbranchid + "'>" + alllabbranches[i].labname + "</option>");             alllabbranches = jquery.grep(alllabbranches, function(value) {               return value != alllabbranches[i];             });           } //end inner if                  } //end inner loop       } //end outer loop     } //end if     (var = 0; < alllabbranches.length; i++) {       $("#labbranchids").append("<option  value='" + alllabbranches[i].labbranchid + "'>" + alllabbranches[i].labname + "</option>");     } //end loop     } //end function 

the problem array element comparison misses element present in array located @ different index in array. error occurs @ following line of code:

if(alllabbranches[i].labbranchid == labsofuser[j].labbranches_labbranchid){ 

kindly me know how compare elements in both array?

the reason why items not matched redefine alllabbranches inside loop on same array, making shorter.

let's assume before change following true:

labsofuser[j-1].labbranches_labbranchid == alllabbranches[i+1].labbranchid 

you expect equality detected later, in next iteration of outer loop. because of re-assignment alllabbranches, value no longer @ index i+1, @ i. value no longer matched during remaining part of inner loop (because matching value @ j-1), , i increment next iteration of outer loop. consequence value never matched.

you solve iterating backwards through alllabbranches array, better create hash values labsofuser array. have better performance.

you can this:

function populateselectedlabs() {     $('#labbranchids').empty();     // create hash     var hash = {};     (var j = 0; j < labsofuser.length; j++) {         hash[labsofuser[j].labbranches_labbranchid] = 1;     }     alllabbranches = jquery.grep(alllabbranches, function(value) {         return hash[value.labbranchid];     }).concat(jquery.grep(alllabbranches, function(value) {         return !hash[value.labbranchid];     }));      (var = 0; < alllabbranches.length; i++) {         $("#labbranchids").append($('<option>')             .val(alllabbranches[i].labbranchid)             .text(alllabbranches[i].labname)             .attr('selected', hash[alllabbranches[i].labbranchid]));     } } 

if target browsers support es6, write this:

function populateselectedlabs() {     $('#labbranchids').empty();     var hash = new set(labsofuser.map( value => value.labbranches_labbranchid ));     alllabbranches.filter( value =>  hash.has(value.labbranchid) )         .concat(alllabbranches.filter( value => !hash.has(value.labbranchid) ))         .foreach( value =>             $("#labbranchids").append($('<option>')                 .val(value.labbranchid)                 .text(value.labname)                 .attr('selected', hash.has(value.labbranchid)))         ); } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -