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

时间:2016-06-13 17:46:33

标签: javascript angularjs

我有两个动态数组。我试过两次合并。我使用了angular.extend和.concat函数。但它对我不起作用。任何人都可以帮助我吗?我的样本数组在这里

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



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

我的输出应该是这样的。

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

2 个答案:

答案 0 :(得分:0)

您需要的功能编程术语称为压缩数组。下面的函数将根据您的示例压缩您的数组。

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;  

};

以下是plunker中的代码:https://plnkr.co/edit/Bwre9HMAL1pnebGnJTlp?p=preview

答案 1 :(得分:0)

检查此功能:

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;
};

参见工作jsfiddle并使用附带的单元测试。