如何获取集合的属性并放入其他对象数组?

How to get attributes of collection and put in other array of objects?

我需要获取 "date"、"id"、"inicial" 和 "final" 的属性以作为对象放入其他数组

我正在尝试使用 lodash,但我仍然无法工作

let all = [
  {
    horas: [
      {
        id_prof: 1,
        inicial: "10:00",
        final: "11:00"
      }
    ],
    id: 1,
    date: "17-08-1993"
  },
  {
    horas: [
      {
        id_prof: 2,
        inicial: "12:00",
        final: "13:00"
      }
    ],
    id: 2,
    date: "18-08-1993"
  },
  {
    horas: [
      {
        id_prof: 3,
        inicial: "10:00",
        final: "11:00"
      }
    ],
    id: 3,
    date: "19-08-1993"
  }
];
 let events = [{}]

我期望这样的结果:

events: [
{id: "value_of_id_prof", title: "{{value_of_inicial}}", start: "value_of_date"},
{the_result_for_each_object_of_all},
{...},
]

尝试

let all = [{
    horas: [{
      id_prof: 1,
      inicial: "10:00",
      final: "11:00"
    }],
    id: 1,
    date: "17-08-1993"
  },
  {
    horas: [{
      id_prof: 2,
      inicial: "12:00",
      final: "13:00"
    }],
    id: 2,
    date: "18-08-1993"
  },
  {
    horas: [{
      id_prof: 3,
      inicial: "10:00",
      final: "11:00"
    }],
    id: 3,
    date: "19-08-1993"
  }
];

const events = all.map(allObj => {
  return {
    id: allObj.id,
    title: allObj.horas[0].inicial,
    start: allObj.date
  };
});

console.log(events);

只要 horas 数组永远只有一个对象,这就可以了。

给你:

let all = [
  {
    horas: [
      {
        id_prof: 1,
        inicial: "10:00",
        final: "11:00"
      }
    ],
    id: 1,
    date: "17-08-1993"
  },
  {
    horas: [
      {
        id_prof: 2,
        inicial: "12:00",
        final: "13:00"
      }
    ],
    id: 2,
    date: "18-08-1993"
  },
  {
    horas: [
      {
        id_prof: 3,
        inicial: "10:00",
        final: "11:00"
      }
    ],
    id: 3,
    date: "19-08-1993"
  }
];
 let events = all.map(x => ({id:x.id, date:x.date, inicial:x.horas[0].inicial, final:x.horas[0].final}))
 console.log(events)

Horas 应该是一个对象,如果你想将它减少到只有一个初始和最终。

另外,你应该post你已经尝试过的东西,总有帮助。

   const all = [
     {
       horas: [
         {
           id_prof: 1,
           inicial: '10:00',
           final: '11:00',
         },
       ],
       id: 1,
       date: '17-08-1993',
     },
     {
       horas: [
         {
           id_prof: 2,
           inicial: '12:00',
           final: '13:00',
         },
       ],
       id: 2,
       date: '18-08-1993',
     },
     {
       horas: [
         {
           id_prof: 3,
           inicial: '10:00',
           final: '11:00',
         },
       ],
       id: 3,
       date: '19-08-1993',
     },
   ];
   
   const result = all.map((r) => {
     const obj = { id: r.id, date: r.date, inicial: r.horas[0].inicial };
     return obj;
   });
   console.log(result);