将属性从一个对象数组添加到另一个对象

时间:2016-09-21 12:30:36

标签: javascript angularjs

我有以下Angular 1.0控制器:

-keepclassmembers class com.dom925.com.example.MyApp {
   public *;
}

-keepattributes SourceFile,LineNumberTable,Signature
-keep class com.parse.*{ *; }
-dontwarn com.parse.**
-dontwarn com.squareup.picasso.**
-keep class * extends com.google.gson.reflect.TypeToken
-keepclasseswithmembernames class * {
    native <methods>;
}
-renamesourcefileattribute SourceFile
-keep public class * extends android.app.Application

-keep class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
    private *;
    void set*(***);
    *** get*();
}
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
-keep class com.example.MyApp.** { *; }

变量vm.events类似于:

function EventListController($timeout, eventService) {
  var vm = this;
  vm.events = getEvents();
  vm.calendar buildCalendar();
}

变量日历如下:

vm.events = [
  { date: "2016-09-22T12:05:42.03", id: 12 }, 
  { date: "2016-09-25T12:05:42.03", id: 14 }
];

我需要浏览所有活动日期,并将ID属性添加到同一天的日历日期。日历周有该月的所有日子。在这种情况下是2016年9月。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您需要迭代这两个对象。您可以使用Date构造函数创建Date对象,并使用setHours(0,0,0,0)将小时,分钟...设置为零,然后您只能比较日期。

var vm = {};
vm.events = [{
  date: "2016-09-22T12:05:42.03",
  id: 12
}, {
  date: "2016-09-25T12:05:42.03",
  id: 14
}];
vm.calendar = {
  weeks: [{
    days: [{
      date: "2016-09-25T09:05:42.03"
    }]
  }]
};

vm.calendar.weeks.forEach(function(w) {
  w.days.forEach(function(d) {
    var e = vm.events.filter(function(e) {
      return new Date(e.date).setHours(0, 0, 0, 0) == new Date(d.date).setHours(0, 0, 0, 0);
    });
    if (e.length) {
      d.id = e[0].id;
    }
    console.log(d)
  })
});

//console.log(vm.calendar.weeks)