通过搜索模式使用多个键在数组中查找和删除

时间:2019-07-02 22:04:56

标签: javascript arrays filter

我正在将PowerShell(PowerCLI)脚本转换为JavaScript ES5代码,以便可以在vRealize Orchestrator中运行它们。我正在尝试查找精简配置的VM列表。在PowerCLI中,我得到了一个名为“ $ result”的对象数组,其中包含以下列-

+----------------+---------------+-------------+---------+------+-------------+  
| VM             | Datastore     | Disk        | Size_GB | Thin | vCenter     |  
+----------------+---------------+-------------+---------+------+-------------+  
| TLAPP021       | L03VM1        | Hard Disk 1 | 40      | True | l03vcenter  |  
| TLSQL003-test  | L03VM1        | Hard Disk 1 | 40      | True | l03vcenter  |  
| RLT00021       | L03VM2        | Hard Disk 2 | 90      | True | l03vcenter  |  
| RLTC0201       | local-L03     | Hard Disk 1 | 40      | True | l03vcenter  |  
| TLAPP022       | L04VM1        | Hard Disk 3 | 140     | True | l04vcenter  |  
| TLAPP022-clone | L04VM1        | Hard Disk 1 | 20      | True | l04vcenter  |  
| TLVMW023       | BRIDGE        | Hard Disk 5 | 90      | True | l03vcenter  |  
| RL000001       | TESTLAB       | Hard Disk 4 | 60      | True | l03vcenter  |  
| RLT00028       | L04VM1        | Hard Disk 1 | 40      | True | l04vcenter  |  
| RLT00028-bku   | L04VM1        | Hard Disk 1 | 40      | True | l04vcenter  |  
| TLADS007       | L03VM2        | Hard Disk 2 | 50      | True | l03vcenter  |  
+----------------+---------------+-------------+---------+------+-------------+ 

然后我根据“ VM”过滤掉这种模式-

$result | where-object {($_.VM -notlike "expand*") -and ($_.VM -notlike "*clone") -and ($_.VM -notlike "*bku") -and ($_.VM -notlike "*test")}

然后我根据“数据存储区”过滤掉了不一样的样式-

$result | where-object {($_.datastore -notlike "*local-*") -and ($_.datastore -notlike "[BRIDGE]*") -and ($_.datastore -notlike "[TESTLAB]*")}

最后我在$ result中得到以下对象-

+----------------+---------------+-------------+---------+------+-------------+  
| VM             | Datastore     | Disk        | Size_GB | Thin | vCenter     |  
+----------------+---------------+-------------+---------+------+-------------+  
| TLAPP021       | L03VM1        | Hard Disk 1 | 40      | True | l03vcenter  |  
| RLT00021       | L03VM2        | Hard Disk 2 | 90      | True | l03vcenter  | 
| TLAPP022       | L04VM1        | Hard Disk 3 | 140     | True | l04vcenter  |  
| RLT00028       | L04VM1        | Hard Disk 1 | 40      | True | l04vcenter  |  
| TLADS007       | L03VM2        | Hard Disk 2 | 50      | True | l03vcenter  |  
+----------------+---------------+-------------+---------+------+-------------+  

然后我先按“ vCenter”,“ VM”,“数据存储”,然后按“磁盘”对$ result排序

这是我现在在JS中想到的-

var Result = [];
var vcs = VcPlugin.allSdkConnections;
for each(var vc in vcs) 
{
    var vms = vc.getAllVirtualMachines()
    for each(var vm in vms) 
    {
        var devices = vm.config.hardware.device;
        for each(var device in devices) 
        {
            if (device.deviceInfo.label.match(/Hard.*/))
            {
                if (device.backing.thinProvisioned)
                {
                    Result.push({
                        VM: vm.name,
                        Datastore: device.backing.fileName.split(" ")[0],
                        Disk: device.deviceInfo.label,
                        Size_GB: (device.capacityInKB/1048576).toFixed(2),
                        Thin: device.backing.thinProvisioned,
                        vCenter: vc.name.split(":")[1].substring(2)
                    });
                }
            }   
        }
    }
}

Result = Result.sort(function(a, b) 
{
    if(a.vCenter < b.vCenter)
    {
        return -1;  
    }
    else if(a.vCenter > b.vCenter)
    {
        return 1;
    }
    else
    {
        if(a.VM < b.VM)
        {
           return -1
        }
        else if(a.VM > b.VM)
        {
          return 1;
        }
        else
        {
            if(a.Datastore < b.Datastore)
            {
               return -1
            }
            else if(a.Datastore > b.Datastore)
            {
              return 1;
            }
            else
            {
                if(a.Disk < b.Disk)
                {
                   return -1
                }
                else if(a.Disk > b.Disk)
                {
                  return 1;
                }
                else
                {
                  return 0;
                }
            }
        }
    }
});

我正在努力用匹配“ VM”和“ Datastore”键的模式从“ Result”变量中过滤掉内容。

我搜索了许多解决方案,但是由于它是ES6或更高版本,因此无法在vRO中运行。我需要一个与ES5兼容的代码。

谢谢!

2 个答案:

答案 0 :(得分:1)

这是一个几乎存在的解决方案(正在讨论中)。请注意,使用Array.prototype.reduce()可以通过两个不同的子查询来简化列表。还请注意在排序功能中将String.prototype.localeCompare()用于字符串比较器。

我对排序结果的期望不清楚。如果您提供所需的排序输出,则可以验证我已实现您的目标。

下面对代码进行注释:

var VMSearchExp = ["^expand", "clone$", "bku$", "test$"];
var DatastoreExp = ["local-L03", "^BRIDGE", "^TESTLAB"];
var data = [
{VM:  "TLAPP021", Datastore: "L03VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "TLSQL003-test", Datastore: "L03VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLT00021", Datastore: "L03VM2",  Disk:"Hard Disk 2",Size_GB:90, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLTC0201", Datastore: "local-L03",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "TLAPP022", Datastore: "L04VM1",  Disk:"Hard Disk 3",Size_GB:140, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLAPP022-clone", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:20, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLVMW023", Datastore: "BRIDGE",  Disk:"Hard Disk 5",Size_GB:90, Thin: true, vCenter: "l03vcenter" },
{VM:  "RL000001", Datastore: "TESTLAB",  Disk:"Hard Disk 4",Size_GB:60, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLT00028", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l04vcenter" },
{VM:  "RLT00028-bku", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLADS007", Datastore: "L03VM2",  Disk:"Hard Disk 2",Size_GB:50, Thin: true, vCenter: "l03vcenter" }
];
// Clone the orginal
var dataCopy = JSON.parse(JSON.stringify(data));
// Reduce the clone to exclude the VMSearchExps
dataCopy = VMSearchExp.reduce(function(acc,exp){dataCopy = dataCopy.filter(d=>!d.VM.match(exp)); return dataCopy}, VMSearchExp[0]);
console.log("Count 8: Originally 11: ",dataCopy);
// Reduce the clone to exclude the DatastoreExps
dataCopy = DatastoreExp.reduce(function(acc,exp){dataCopy = dataCopy.filter(d=>!d.Datastore.match(exp)); return dataCopy},DatastoreExp[0]);
console.log("Count 5: Originally 8: ",dataCopy);
// Sort: if any of a.[vCenter,VM,Datastore,Disk] comes before any of those in b -> promote it
result = dataCopy.sort(function(a,b) {
                         a.vCenter.localeCompare(b.vCenter) || 
                         a.VM.localeCompare(b.VM) || 
                         a.Datastore.localeCompare(b.Datastore) || 
                         a.Disk.localeCompare(b.Disk)
                        });
// Sorted result                        
console.log("Sorted: ",result);

答案 1 :(得分:0)

感谢兰迪!我使用Bable将您的代码转换为ES5,并进行了一些优化-

var VMSearchExp = ["^expand", "clone$", "bku$", "test$"];
var DatastoreExp = ["^local", "^BRIDGE", "^TESTLAB"];
var data = [
{VM:  "TLAPP021", Datastore: "L03VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "TLSQL003-test", Datastore: "L03VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLT00021", Datastore: "L03VM2",  Disk:"Hard Disk 2",Size_GB:90, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLTC0201", Datastore: "local-L03",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l03vcenter" },
{VM:  "TLAPP022", Datastore: "L04VM1",  Disk:"Hard Disk 3",Size_GB:140, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLAPP022-clone", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:20, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLVMW023", Datastore: "BRIDGE",  Disk:"Hard Disk 5",Size_GB:90, Thin: true, vCenter: "l03vcenter" },
{VM:  "RL000001", Datastore: "TESTLAB",  Disk:"Hard Disk 4",Size_GB:60, Thin: true, vCenter: "l03vcenter" },
{VM:  "RLT00028", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l04vcenter" },
{VM:  "RLT00028-bku", Datastore: "L04VM1",  Disk:"Hard Disk 1",Size_GB:40, Thin: true, vCenter: "l04vcenter" },
{VM:  "TLADS007", Datastore: "L03VM2",  Disk:"Hard Disk 2",Size_GB:50, Thin: true, vCenter: "l03vcenter" }
];

var dataCopy = JSON.parse(JSON.stringify(data));

dataCopy = VMSearchExp.reduce(function (acc,exp) 
{
    dataCopy = dataCopy.filter(function (d) 
    {
        return !d.VM.match(exp);
    })
    return dataCopy;
}, VMSearchExp[0]);

dataCopy = DatastoreExp.reduce(function (acc,exp)
{
    dataCopy = dataCopy.filter(function (d)
    {
        return !d.Datastore.match(exp);
    })
    return dataCopy;
}, DatastoreExp[0]);

var result = dataCopy.sort(function (a,b)
{
    return a.vCenter.localeCompare(b.vCenter) || 
    a.VM.localeCompare(b.VM) || 
    a.Datastore.localeCompare(b.Datastore) || 
    a.Disk.localeCompare(b.Disk);
});

这是输出-

[INFO] Input:
[INFO] VM:- TLAPP021 Datastore:- L03VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLSQL003-test Datastore:- L03VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00021 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLTC0201 Datastore:- local-L03 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLAPP022 Datastore:- L04VM1 Disk:- Hard Disk 3 Size_GB:- 140 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLAPP022-clone Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 20 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLVMW023 Datastore:- BRIDGE Disk:- Hard Disk 5 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RL000001 Datastore:- TESTLAB Disk:- Hard Disk 4 Size_GB:- 60 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00028 Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l04vcenter
[INFO] VM:- RLT00028-bku Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLADS007 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 50 Thin:- true vCenter:- l03vcenter
[INFO] Count 8: Originally 11: 
[INFO] VM:- TLAPP021 Datastore:- L03VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00021 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLTC0201 Datastore:- local-L03 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLAPP022 Datastore:- L04VM1 Disk:- Hard Disk 3 Size_GB:- 140 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLVMW023 Datastore:- BRIDGE Disk:- Hard Disk 5 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RL000001 Datastore:- TESTLAB Disk:- Hard Disk 4 Size_GB:- 60 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00028 Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLADS007 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 50 Thin:- true vCenter:- l03vcenter
[INFO] Count 5: Originally 8: 
[INFO] VM:- TLAPP021 Datastore:- L03VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00021 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLAPP022 Datastore:- L04VM1 Disk:- Hard Disk 3 Size_GB:- 140 Thin:- true vCenter:- l04vcenter
[INFO] VM:- RLT00028 Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLADS007 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 50 Thin:- true vCenter:- l03vcenter
[INFO] Sorted: 
[INFO] VM:- RLT00021 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 90 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLADS007 Datastore:- L03VM2 Disk:- Hard Disk 2 Size_GB:- 50 Thin:- true vCenter:- l03vcenter
[INFO] VM:- TLAPP021 Datastore:- L03VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l03vcenter
[INFO] VM:- RLT00028 Datastore:- L04VM1 Disk:- Hard Disk 1 Size_GB:- 40 Thin:- true vCenter:- l04vcenter
[INFO] VM:- TLAPP022 Datastore:- L04VM1 Disk:- Hard Disk 3 Size_GB:- 140 Thin:- true vCenter:- l04vcenter
 => undefined