简体   繁体   English

如何在angularjs中合并两个数组?

[英]how to merge two array in angularjs?

I have two dynamic arrays. 我有两个动态数组。 I tried two merge both. 我尝试了两个合并。 I used angular.extend and .concat function. 我使用了angular.extend和.concat函数。 But It does not work for me. 但这对我不起作用。 Can anyone help me out from this? 有人可以帮我这个忙吗? My sample array is here 我的样本数组在这里

 "first_data": [                             
            [
                "JSP",
                4463
            ],               
            [
                "JAVA",
                2022
            ]
        ],



          "second_data": [                              
            [
                "JSP",
                2483
            ],
            [
                "HTTP CONNECTION",
                43224
            ],
            [
                "JS",
                27413
            ]
        ]

My output should be like this. 我的输出应该是这样的。

 "output_data": [                              
            [
                "JSP",
                4463,
                2483
            ],               
            [
                "JAVA",
                2022,
                null
            ],
             [
                "HTTP CONNECTION",
                null,
                43224
            ],
            [
                "JS",
                null,
                27413
            ]
        ]

What you require is known in functional programming terms as a zipped array. 在功能编程术语中,您所需要的称为压缩数组。 The function below will zip your arrays as per your example. 下面的函数将按照您的示例压缩数组。

Use it like this var newArray = zipArrays(arrays.first_data,arrays.second_data); 像这样使用它var newArray = zipArrays(arrays.first_data,arrays.second_data);

var zipArrays = function(array_one, array_two) {

    //first take the first array and map any id from the second array to it
    var result = array_one.map(

      function(arrayItem) {
        //default setting = not found
        var found = -1;

        //search the second array for the current item in the first array
        array_two.forEach(function(secondArrayItem, index) {
          if (arrayItem[0] === secondArrayItem[0]) {
            found = index;
          }
        });

        //if we have found a match from the second array, add the id from the second array to the corresponding item in the first array
        if (found !== -1) {
          arrayItem.push(array_two[found][1]);
        } else {
          //no match in the second array, so add id as null
          arrayItem.push(null);
        }

        return arrayItem;
      }
    );

  //search the second array for items not in the first array
  array_two.forEach(

    function(arrayItem) {

      var found = -1;

      //the search of the first array              
      result.forEach(function(resultItem, index) {
        if (arrayItem[0] === resultItem[0]) {
          found = index;
        }

      });

      //item wasn't in the first array, so we'll add it to our results, and add null as the first id (since it wasn't in the first array)
      if (found === -1) {
        arrayItem.splice(1, 0, null);
        result.push(arrayItem);
      }
    }
  );

  return result;  

};

Here is the code in a plunker: https://plnkr.co/edit/Bwre9HMAL1pnebGnJTlp?p=preview 这是代码插入器中的代码: https ://plnkr.co/edit/Bwre9HMAL1pnebGnJTlp ? p = preview

Check this function: 检查此功能:

mergeArrays = function(arr1, arr2) {
    var result = [];
    var array1 = JSON.parse(JSON.stringify(arr1));
    var array2 = JSON.parse(JSON.stringify(arr2));

    for (var ind1 = 0; ind1 < array1.length; ind1++) {
        result.push(array1[ind1]);
    }

    var wasInSecond = false;
    for (var ind1 = 0; ind1 < array1.length; ind1++) {
        wasInSecond = false;

        for (var ind2 = 0; ind2 < array2.length; ind2++) {
            if (array1[ind1][0] == array2[ind2][0]) {
                result[ind1].push(array2[ind2][1]);
                wasInSecond = true;
            }
        }
        if (!wasInSecond) {
            result[ind1].push(null);
        }
    }

    var wasInFirst = false;
    for (var ind2 = 0; ind2 < array2.length; ind2++) {
        wasInFirst = false;
        for (var ind1 = 0; ind1 < array1.length; ind1++) {
            if (array2[ind2][0] === array1[ind1][0]) {
                wasInFirst = true;
            }
        }
        if (!wasInFirst) {
            var toAdd = [array2[ind2][0], null, array2[ind2][1]];
            result.push(toAdd);
        }
    }

    return result;
};

See working jsfiddle and play with included unit tests. 请参阅jsfiddle,并进行包含的单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM