使用angularjs将一个数组数据添加到另一个数组中

时间:2016-06-29 13:48:59

标签: javascript arrays angularjs

我有一个名为

的数组
workflow_column :status

它可以存储一周中的几天。

我有另一个数组,写了一周的所有日子。

$scope.workingSchedules=[
                       {
                  workingDay:"MONDAY",
                  workingHours:[{fromTime:'1222',toTime:'1300'}]
                      },
                  workingDay:"MONDAY",
                  workingHours:[{fromTime:'1222',toTime:'1300'}]
                     ];

我想要的是将第一个数组(workingSchedules [])数据添加到第二个数组(workingTime [])中,并保持不常见的workDays,因为它在第二个数组中。第一个数组可以有多天。

这是我的代码:

$scope.workingTime = [ 
        {   workingDay: 'MONDAY',
            workingHours: []
        },
        {   workingDay: 'TUESDAY',
            workingHours: []
        },
        {   workingDay: 'WEDNESDAY',
            workingHours: []
        },
        {   workingDay: 'THURSDAY',
            workingHours: []
        },
        {   workingDay: 'FRIDAY',
            workingHours: []
        },
        {   workingDay: 'SATURDAY',
            workingHours: []
        },
        {   workingDay: 'SUNDAY',
            workingHours: []
        }
     ];

感谢任何帮助,谢谢

2 个答案:

答案 0 :(得分:0)

我在这里看到了几个问题。当您尝试向其插入键/值时,您的第一个数组无效。将其声明为对象或将其所有项目都设置为对象,如下所示: Console console = System.console(); if (console == null) { System.out.println("Couldn't get Console instance"); System.exit(0); } char passwordArray[] = console.readPassword("Enter your password: "); console.printf("Password entered was: %s%n", new String(passwordArray)); 而不是{workingDay:"MONDAY"},

然后你的循环被奇怪地编入索引。请尝试使用workingDay:"MONDAY",代替i<$scope.workingTime.length

答案 1 :(得分:0)

执行$scope.workingTime[i]=$scope.workingSchedules[j];时,您正在替换该值。您应该使用.concat来合并值。

具有重复条目的解决方案

var data=[{workingDay:"MONDAY",workingHours:[]},{workingDay:"TUESDAY",workingHours:[]},{workingDay:"WEDNESDAY",workingHours:[]},{workingDay:"THURSDAY",workingHours:[]},{workingDay:"FRIDAY",workingHours:[]},{workingDay:"SATURDAY",workingHours:[]},{workingDay:"SUNDAY",workingHours:[]}]
var workingSchedules=[{workingDay:"MONDAY",workingHours:[{fromTime:"1222",toTime:"1300"}]},{workingDay:"MONDAY",workingHours:[{fromTime:"1222",toTime:"1300"}]}];

data.forEach(function(o) {
  var f = workingSchedules.filter(function(item) {
    return item.workingDay === o.workingDay;
  });
  if (f.length)
    o.workingHours = o.workingHours.concat(f.reduce(function(p, c) {
      return p.workingHours.concat(c.workingHours)
    }));
});

console.log(data)

具有唯一值的解决方案

var data=[{workingDay:"MONDAY",workingHours:[]},{workingDay:"TUESDAY",workingHours:[]},{workingDay:"WEDNESDAY",workingHours:[]},{workingDay:"THURSDAY",workingHours:[]},{workingDay:"FRIDAY",workingHours:[]},{workingDay:"SATURDAY",workingHours:[]},{workingDay:"SUNDAY",workingHours:[]}];
var workingSchedules=[{workingDay:"MONDAY",workingHours:[{fromTime:"1222",toTime:"1300"}]},{workingDay:"MONDAY",workingHours:[{fromTime:"1222",toTime:"1300"}]},{workingDay:"MONDAY",workingHours:[{fromTime:"1400",toTime:"1700"}]}];

data.forEach(function(o) {
  var f = workingSchedules.filter(function(item) {
    return item.workingDay === o.workingDay;
  });
  if (f.length) {
    o.workingHours = o.workingHours.concat(f.reduce(function(p, c) {
      var exists = p.some(function(item) {
        return item.fromTime === c.workingHours[0].fromTime && item.toTime === c.workingHours[0].toTime;
      });
      return exists ? p : p.concat(c.workingHours);
    }, []));
  }
});

console.log(data)