开发者问题收集

尝试合并两个数组

2017-02-21
85

下面是我存储在中的 json:

$scope.regions = [];

{
  "id": 100,
  "regions": [
    {
      "id": 10,
      "name": "Abc",
      "rank": 0,
    },
    {
      "id": 20,
      "name": "Pqr",
      "rank": 1,
    },
    {
      "id": 30,
      "name": "Lmn",
      "rank": 2,
    },
    {
      "id": 40,
      "name": "xyz",
      "rank": 3,
    },
    {
      "id": 50,
      "name": "GGG",
      "rank": 4,
    },
    {
      "id": 60,
      "name": "YYY",
      "rank": 5,
    }
  ]
}

这是我存储在中的另一个 json:

$scope.regionList = [];
var highestOrder = 3;

  "regions": [
    {
      "id": 40,
      "name": "xyz",
      "rank": 0,
    },
    {
      "id": 50,
      "name": "GGG",
      "rank": 1,
    },
    {
      "id": 60,
      "name": "YYY",
      "rank": 2,
    }

现在我想将 $scope.regionList 合并到 $scope.regions 中,但是对于那些在 $scope.regionList 和 $scope.regions 中都匹配的记录,我想用 $scope.regionList 替换 $scope.regions 的记录(仅来自两个列表的公共记录)。

并且 $scope.regionList 中第一个不匹配的记录将使用 highestOrder 开始排序,并将对每个不匹配的记录不断递增,因此最终输出将如下所示:

预期输出:

"regions": [
    {
      "id": 10,
      "name": "Abc",
      "rank": 3,
    },
    {
      "id": 20,
      "name": "Pqr",
      "rank": 4,
    },
    {
      "id": 30,
      "name": "Lmn",
      "rank": 5,
    },
    {
      "id": 40,
      "name": "xyz",
      "rank": 0,
    },
    {
      "id": 50,
      "name": "GGG",
      "rank": 1,
    },
    {
      "id": 60,
      "name": "YYY",
      "rank": 2,
    }

由于 Abc 是第一个不匹配的记录,因此它的顺序为 3,其余的顺序号从 3 开始,即 4、5 6,等。

我的代码:

var highestOrder = 3;
 var found = false;

 for (var i = 0; i < $scope.regions.length; i++) {
   if ($scope.regions[i].id == 100) {
       found = true;
       for (var j = 0; j < $scope.regionList.length; j++) {
          for (var k = 0; k < $scope.regions[i].regions.length; k++) {
           if ($scope.regions[i].regions[k].id == $scope.regionList[j].id) {
                   $scope.regions[i].regions[k].rank = $scope.regionList[j].rank;
             } 
            else {
                   $scope.regions[i].regions[k].rank = highestOrder;
                    highestOrder = highestOrder + 1;
                 }
         }
     }
  }
  if (found)
       break;
}
var regions = {
  "id": 100,
  "regions": [{
      "id": 10,
      "name": "Abc",
      "rank": 0,
    },
    {
      "id": 20,
      "name": "Pqr",
      "rank": 1,
    },
    {
      "id": 30,
      "name": "Lmn",
      "rank": 2,
    },
    {
      "id": 40,
      "name": "xyz",
      "rank": 3,
    },
    {
      "id": 50,
      "name": "GGG",
      "rank": 4,
    },
    {
      "id": 60,
      "name": "YYY",
      "rank": 5,
    }
  ]
}
var highestOrder = 3;
var found = false;
var regionList = [{
    "id": 40,
    "name": "xyz",
    "rank": 0,
  },
  {
    "id": 50,
    "name": "GGG",
    "rank": 1,
  },
  {
    "id": 60,
    "name": "YYY",
    "rank": 2
  }
]

for (var i = 0; i < regions.length; i++) {
  if (regions[i].id == 100) {
    found = true;
    for (var j = 0; j < regionList.length; j++) {
      for (var k = 0; k < regions[i].regions.length; k++) {
        if (regions[i].regions[k].id == regionList[j].id) {
          regions[i].regions[k].rank = regionList[j].rank;
        } else {
          regions[i].regions[k].rank = highestOrder;
          highestOrder = highestOrder + 1;
        }
      }
    }
  }
  if (found)
    break;
}
console.log(regions)
1个回答

您可以使用哈希表并使用数组元素构建它以进行更新。

然后迭代 regions 并使用哈希的 rankhighestOrder 更新 rank 。分配后增加 highestOrder

var $scope = { regions: [{ id: 100, regions: [{ id: 10, name: "Abc", rank: 0, }, { id: 20, name: "Pqr", rank: 1, }, { id: 30, name: "Lmn", rank: 2, }, { id: 40, name: "xyz", rank: 3, }, { id: 50, name: "GGG", rank: 4, }, { id: 60, name: "YYY", rank: 5, }] }] },
    regionsUpdate = [{ id: 40, name: "xyz", rank: 0, }, { id: 50, name: "GGG", rank: 1, }, { id: 60, name: "YYY", rank: 2, }],
    regionsId = 100,
    highestOrder = 3,
    hash = Object.create(null);

regionsUpdate.forEach(function (a) {
    hash[a.id] = a;
});

$scope.regions.some(function (a) {
    if (a.id === regionsId) {
        a.regions.forEach(function (b) {
            b.rank = hash[b.id] ? hash[b.id].rank : highestOrder++;
        });
        return true;
    }
});

console.log($scope);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
2017-02-21