javascript - Trying to filter out matching records from 2 array as 1 array have different structure -


below sample records :

var array1 = [     {       "testid": 15,       "child": [         {           "variantid": 100,           "name": "a1",         },         {           "variantid": 200,           "name": "a2",         },         {           "variantid": 300,           "name": "a3",         },         {           "variantid": 400,           "name": "a4",         },         {           "variantid": 500,           "name": "a5",         }       ]     } ]   var array2= [     {       "variantid": 100,       "tests": [         {           "testid": 15,           "flag" : true         }       ]     },     {       "variantid": 200,       "tests": [         {           "testid": 15,           "flag" : false         }       ]     },     {       "variantid": 400,       "tests": [         {           "testid": 15,           "flag" : true         }       ]     },     {       "variantid": 500,       "tests": [         {           "testid": 15,           "flag" : true         }       ]     }   ] 

at first fetch selected testid record array1.now select variantid array2 flag true.for eg:from inputs ,variantids 100,400 , 500.

now selected variantsids array2 want fetch matching variantids array2 want search 100,400 , 500 in array1 , select matching records array1.

expected output :

"child": [         {           "variantid": 100,           "name": "a1",         },         {           "variantid": 400,           "name": "a4",         },         {           "variantid": 500,           "name": "a5",         }       ] 

so far successfull in getting specific testid record array1 not getting how filter out variantids array1 structure of array2 little complex.

can please me this?

var array1 = [      {        "testid": 15,        "child": [          {            "variantid": 100,            "name": "a1",          },          {            "variantid": 200,            "name": "a2",          },          {            "variantid": 300,            "name": "a3",          },          {            "variantid": 400,            "name": "a4",          },          {            "variantid": 500,            "name": "a5",          }        ]      }  ]    var array2=  [      {        "variantid": 100,        "tests": [          {            "testid": 14,            "flag" : true          },          {            "testid": 15,            "flag" : true          }        ]      },      {        "variantid": 200,        "tests": [          {            "testid": 14,            "flag" : true          },          {            "testid": 15,            "flag" : false          }        ]      },      {        "variantid": 400,        "tests": [          {            "testid": 14,            "flag" : true          },          {            "testid": 15,            "flag" : true          }        ]      },      {        "variantid": 500,        "tests": [          {            "testid": 14,            "flag" : true          },          {            "testid": 15,            "flag" : true          }        ]      }    ]          var testid = 15;    var testobj ={};   (var = 0; < array1.length; i++) {              if (array1[i].testid == testid) {                  testobj = array1[i];                  break;              }          }          console.log(testobj);

you use hash table , first true flags, filter child array.

function getchildren(data, flags, testid) {      var hash = object.create(null),          result;        flags.foreach(function (a) {          a.tests.foreach(function (b) {              b.testid === testid && b.flag && (hash[a.variantid] = true);          });      });        data.foreach(function (a) {          if (a.testid === testid) {              result = {                  child: a.child.filter(function (b) {                      return hash[b.variantid];                  })              };          }      });      return result;  }    var array1 = [{ testid: 15, child: [{ variantid: 100, name: "a1", }, { variantid: 200, name: "a2", }, { variantid: 300, name: "a3", }, { variantid: 400, name: "a4", }, { variantid: 500, name: "a5", }] }],      array2 = [{ variantid: 100, tests: [{ testid: 15, flag: true }] }, { variantid: 200, tests: [{ testid: 15, flag: false }] }, { variantid: 400, tests: [{ testid: 15, flag: true }] }, { variantid: 500, tests: [{ testid: 15, flag: true }] }],      array3 = [{ testid: 15, child: [{ variantid: 100, name: "a1", }, { variantid: 200, name: "a2", }, { variantid: 300, name: "a3", }, { variantid: 400, name: "a4", }, { variantid: 500, name: "a5", }] }],      array4 = [{ variantid: 100, tests: [{ testid: 14, flag: true }, { testid: 15, flag: true }] }, { variantid: 200, tests: [{ testid: 14, flag: true }, { testid: 15, flag: false }] }, { variantid: 400, tests: [{ testid: 14, flag: true }, { testid: 15, flag: true }] }, { variantid: 500, tests: [{ testid: 14, flag: true }, { testid: 15, flag: true }] }],      testid = 15;    console.log(getchildren(array1, array2, testid));  console.log(getchildren(array3, array4, testid));
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -