如何比较二维数组和一维数组以及在Java脚本中存储另一个数组中的公共数据

时间:2022-09-06 12:13:58

I have 2 Arrays and one is 2 dimensional and another is 1 dimensional. I need to compare both and need to store there common data in another array. I tried the below approach:-

我有2个阵列,一个是2维,另一个是1维。我需要比较两者并需要在另一个数组中存储公共数据。我尝试了以下方法: -

tw.local.listtodisplayNW = new tw.object.listOf.listtodisplayNWBO();
//if(tw.local.SQLResults[0].rows.listLength >
//    tw.local.virtualServers.listLength)
var k=0;
for (var i=0;i<tw.local.SQLResults[0].rows.listLength;i++)  
{
log.info("Inside SQLResults loop - For RuntimeID: " 
+tw.local.SQLResults[0].rows[i].data[3]);
for(var j=0;j<tw.local.virtualServers.listLength;j++)
{
    log.info("Inside API loop - For RuntimeID: " 
+tw.local.virtualServers[j].runtimeid);
    if(tw.local.SQLResults[0].rows[i].data[3] == 
tw.local.virtualServers[j].runtimeid)
    {
        tw.local.listtodisplayNW[k] = new tw.object.listtodisplayNWBO();
        tw.local.listtodisplayNW[k].vsysName = 
tw.local.virtualServers[j].virtualSystemName;
        tw.local.listtodisplayNW[k].vsysID = 
tw.local.virtualServers[j].virtualSystemId;
        tw.local.listtodisplayNW[k].serverName = 
tw.local.virtualServers[j].serverName;
        tw.local.listtodisplayNW[k].serverID = 
tw.local.virtualServers[j].serverId;
        tw.local.listtodisplayNW[k].runtimeID = 
tw.local.virtualServers[j].runtimeid;
        //tw.local.listtodisplayNW[k].IPAddress = 
tw.local.virtualServers[j].nics[j].ipAddress;
        log.info("VsysName: 
"+tw.local.listtodisplayNW[k].vsysName+"RuntimeID: 
"+tw.local.listtodisplayNW[k].runtimeID);
        //tw.local.listtodisplayNW[k] = new 
tw.object.listtodisplayNWBO();
        tw.local.listtodisplayNW[k].currentSpeed = 
tw.local.SQLResults[0].rows[i].data[5];
        log.info("VsysName: 
"+tw.local.listtodisplayNW[k].vsysName+"RuntimeID: 
"+tw.local.listtodisplayNW[k].runtimeID+"CurrentSpeed: 
"+tw.local.listtodisplayNW[k].currentSpeed);
        if(tw.local.listtodisplayNW[k].currentSpeed != "100 Mbps")
        {
           tw.local.listtodisplayNW[k].desiredSpeed = "100 Mbps";
         }
        else
        {
           tw.local.listtodisplayNW[k].desiredSpeed = "1 Gbps";
        }
        log.info("DesiredSpeed: 
 "+tw.local.listtodisplayNW[k].desiredSpeed);
        k++; 
     }

 }
 log.info("Length of 
 listtodisplayNW"+tw.local.listtodisplayNW.listLength);
 } 

In above code SQLResults is a 2-d array and virtualServers is a 1-D array. I need to compare both these array and common data need to be store in another array. Here performance is not good. Is there any other way to do this efficiently. Please make a needful favour and Thanks in advance.

在上面的代码中,SQLResults是一个二维数组,而virtualServers是一维数组。我需要比较这些数组和普通数据需要存储在另一个数组中。这里的表现并不好。有没有其他方法可以有效地做到这一点。请提出需要帮助,并提前致谢。

2 个解决方案

#1


0  

Assuming integer data, the following example works on the theme of array implementation of set intersection, which will take care of performance.

假设整数数据,下面的示例适用于集合交集的数组实现的主题,它将处理性能。

  1. Convert 2D array to 1D.

    将2D数组转换为1D。

    var 2DtoIDArray =  2DArray.join().split(",");
    
  2. Create an array named marker whose purpose is to serve as a lookup that element. This needs to be done as follows.

    创建一个名为marker的数组,其目的是作为查找该元素。这需要按如下方式完成。

    Iterate through the smaller array, say 1DArray and keep setting marker as follows throughout iteration.

    迭代通过较小的数组,比如1DArray并在整个迭代过程中保持设置标记如下。

    marker[1DArray[counter]]='S1';
    

    Now iterate through 2Dto1DArray array(you may use nested loop iteration if you dont want to convert it to 1 dimesnional) and for each element of this array check if its marked as 'S1' in the marker lookup array. If yes, keep adding the elements in the commonElementsArray.

    现在迭代2Dto1DArray数组(如果你不想将它转换为1个二维,你可以使用嵌套循环迭代)并且对于这个数组的每个元素检查它是否在标记查找数组中标记为“S1”。如果是,请继续在commonElementsArray中添加元素。

#2


0  

Follow this simple approach

遵循这个简单的方法

  1. Since the matching condition is only one between the two large arrays, create two maps (one for each array) to map each record against that attribute which is to be matched
  2. 由于匹配条件只是两个大数组之间的匹配条件,因此创建两个映射(每个数组一个)以将每个记录映射到要匹配的属性

For SQLResults

var map1 = {};
tw.local.SQLResults[0].rows.each( function(row){
  map1[ row.data[3] ] = row;
});

and similarly for virtual servers

和虚拟服务器类似

var map2 = {};
tw.local.virtualServers.each( function(vs){
  map2[ vs.runtimeid ] = vs;
});
  1. Now iterate these two maps wrt to their keys and set the values in new array
  2. 现在将这两个映射wrt迭代到它们的键并在新数组中设置值

new array being tw.local.listtodisplayNW

新数组是tw.local.listtodisplayNW

tw.local.listtodisplayNW = [];
Object.keys( map1 ).forEach( function( key ){
   if( map2[ key ] )
   {
     //set the values in tw.local.listtodisplayNW 
   }
})

Complexity of the approach is simply O(n) since there is no nested loops.

由于没有嵌套循环,因此方法的复杂性仅为O(n)。

#1


0  

Assuming integer data, the following example works on the theme of array implementation of set intersection, which will take care of performance.

假设整数数据,下面的示例适用于集合交集的数组实现的主题,它将处理性能。

  1. Convert 2D array to 1D.

    将2D数组转换为1D。

    var 2DtoIDArray =  2DArray.join().split(",");
    
  2. Create an array named marker whose purpose is to serve as a lookup that element. This needs to be done as follows.

    创建一个名为marker的数组,其目的是作为查找该元素。这需要按如下方式完成。

    Iterate through the smaller array, say 1DArray and keep setting marker as follows throughout iteration.

    迭代通过较小的数组,比如1DArray并在整个迭代过程中保持设置标记如下。

    marker[1DArray[counter]]='S1';
    

    Now iterate through 2Dto1DArray array(you may use nested loop iteration if you dont want to convert it to 1 dimesnional) and for each element of this array check if its marked as 'S1' in the marker lookup array. If yes, keep adding the elements in the commonElementsArray.

    现在迭代2Dto1DArray数组(如果你不想将它转换为1个二维,你可以使用嵌套循环迭代)并且对于这个数组的每个元素检查它是否在标记查找数组中标记为“S1”。如果是,请继续在commonElementsArray中添加元素。

#2


0  

Follow this simple approach

遵循这个简单的方法

  1. Since the matching condition is only one between the two large arrays, create two maps (one for each array) to map each record against that attribute which is to be matched
  2. 由于匹配条件只是两个大数组之间的匹配条件,因此创建两个映射(每个数组一个)以将每个记录映射到要匹配的属性

For SQLResults

var map1 = {};
tw.local.SQLResults[0].rows.each( function(row){
  map1[ row.data[3] ] = row;
});

and similarly for virtual servers

和虚拟服务器类似

var map2 = {};
tw.local.virtualServers.each( function(vs){
  map2[ vs.runtimeid ] = vs;
});
  1. Now iterate these two maps wrt to their keys and set the values in new array
  2. 现在将这两个映射wrt迭代到它们的键并在新数组中设置值

new array being tw.local.listtodisplayNW

新数组是tw.local.listtodisplayNW

tw.local.listtodisplayNW = [];
Object.keys( map1 ).forEach( function( key ){
   if( map2[ key ] )
   {
     //set the values in tw.local.listtodisplayNW 
   }
})

Complexity of the approach is simply O(n) since there is no nested loops.

由于没有嵌套循环,因此方法的复杂性仅为O(n)。