如何转换两个数组

时间:2018-05-21 12:21:37

标签: javascript arrays angularjs json

转换的最佳方式是什么:

["Isolated-1", "SVT_FedPortGroup", "SVT_StoragePortGroup", "VM Network", "test-pg-2002"]

和此:

["target_Isolated-1", "target_SVT_FedPortGroup", "target_SVT_StoragePortGroup","target_VM Network" ,"target_test-pg-2002"]; 

为:

"NetworkMaps": [
    {
        "ENVID": null,
        "SourcePG": "Isolated-1",
        "TargetPG": "target_Isolated-1"
    },
    {
        "ENVID": null,
        "SourcePG": "VM Network",
        "TargetPG": "target_SVT_FedPortGroup"
    }...
]

我需要将两个数组合并为各自的值。 例如

 arr1 : ["a", "b", "c"]; 
 arr2 : ["apple", "ball", "cat"];

result : [{source: "a",target: "apple"}, {source: "b",target: "ball"},{source: "c",target: "cat"}]

1 个答案:

答案 0 :(得分:0)

只需在一个简单的循环中按索引(如果它们具有相同的长度)映射它们。这是一个演示:



.json()