javascript中的多对多映射对象



我有两组数组:

$scope.selectedEmployees = ["1001", "1002"];
$scope.selectedTasks = ["Task1", "Task2"];

我想通过像多对多关系那样组合员工和任务来生成一个对象数组。$scope的长度。select employees和$scope。selectedTasks可能不同:

newArray=[
    {
        "empId": 1001,
        "task": "Task1"
    },
    {
        "empId": 1001,
        "task": "Task2"
    },
    {
        "empId": 1002,
        "task": "Task2"
    },
    {
        "empId": 1002,
        "task": "Task2"
    }
]

我尝试的方法:

 var newArray=[];
         for (var i = 0; i <$scope.selectedEmployees.length; i++) {
           for (var j = 0; j <$scope.selectedTasks .length; j++) {
    newArray.push({"empId":$scope.selectedEmployees[i],
                  "task":$scope.selectedIntervention[j]
                 })
                   }
                }

但是我不能得到所需的格式。

http://jsfiddle.net/zuv8y9wk/

用selectedIntervention代替selectedTasks

var $scope = {};
$scope.selectedEmployees = ["1001", "1002"];
$scope.selectedTasks = ["Task1", "Task2"];
var newArray = [];
for (var i = 0; i < $scope.selectedEmployees.length; i++) {
    for (var j = 0; j < $scope.selectedTasks.length; j++) {
        newArray.push({
            "empId": $scope.selectedEmployees[i],
            "task": $scope.selectedTasks[j]
        })
    }
}
console.log(newArray)

相关内容

  • 没有找到相关文章

最新更新