简体   繁体   English

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

[英]Add properties from one array of objects to another

I have the following Angular 1.0 controller: 我有以下Angular 1.0控制器:

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

The variable vm.events is something like: 变量vm.events类似于:

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

The variable calendar is the following: 可变日历如下:

vm.calendar = {
  weeks: [
    { days: [ { date: "2016-09-12T12:05:42.03" }, ... ] }
    { days: [ { date: "2016-09-22T12:05:42.03" }, ... ] }
  ]
}

I need to go through all events dates and add the ID property to the calendar date of the same DAY. 我需要检查所有事件的日期,并将ID属性添加到同一天的日历日期。 Calendar weeks has all days of that month. 日历周包含该月的所有日子。 In this case is September 2016. 在这种情况下是2016年9月。

How can I do this? 我怎样才能做到这一点?

You need to iterate both objects. 您需要迭代两个对象。 You can use Date constructor to create Date object and the use setHours(0,0,0,0) to set hours, mins.. as zero, then you can only compare date. 您可以使用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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个对象数组创建一个对象数组,并为其添加更多属性 - Create an array of objects from another array of objects and add more properties to it JavaScript:有条件地将JSON对象从一个数组添加到另一个数组 - JavaScript: Add JSON objects from one array to another array conditionally 如何在 typescript 中将一些属性从一个数组添加到另一个数组 - How to add some properties from one array to another in typescript 如何将一个对象数组的属性转换为另一个对象数组 - How to get properties of one array of objects into another array of objects 将属性从单独的数组添加到对象数组 - Add properties to an array of objects from a separate array 根据值将一个带有对象的数组中的属性添加到另一个数组 - Add property from one array with objects to another based on value 从对象数组中提取属性并存储在另一个对象数组中 - extract properties from array of objects and store in another array of objects 如果一个属性等于另一个 object 的属性,如何过滤位于对象数组中的 object 的少数属性 - How to filter few properties of an object which is in Array of objects if one property equals property from another object 向具有不同长度的另一个数组的对象数组添加新属性,因此一旦数组完成迭代,它将从第一个开始 - Add new properties to an array of objects from another array with different lengths so once the array is done iterating, it starts from first again 将一个数组中具有相同 id 的所有对象添加到另一个数组中具有相同 id 的 object 的数组中 - Add all objects with the same id from one array into an array of an object with the same id in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM