将对象数组转换为包含对象数组的对象(复杂)

时间:2018-06-02 06:39:58

标签: javascript arrays react-native javascript-objects

我正在使用React Native Calendars并尝试为议程组件构建数据。

预期的数据结构是(对象)

{
 '2012-05-22': [{text: 'item 1 - any js object'}],
 '2012-05-23': [{text: 'item 2 - any js object'}],
 '2012-05-24': [],
 '2012-05-25': [{text: 'item 3 - any js object'},{text: 'any js object'}],
}`

从我的api中获取事件时,事件以下列格式返回(对象数组)`

 let eventsFetchedFromApi = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Third Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Second Event"},
 ]`

我已经设法使用以下代码将我的arrray对象转换为一个对象(不幸的是它不是非常易读,至少对我来说不是)。

let transformedEvents = Object.assign(...eventsFetchedFromApi.map(({ startDateTime, comments }) => ({ [startDateTime.substring(0, 10)]: [{comments: [comments]}] })));  

'transformedEvents'的结构是(ALMOST THERE)

{
"2018-02-01":[{"comments": ["First Event"]}],
"2018-03-01":[{"comments": ["Third Event"]}]
}

如上所示,仅显示3个事件中的2个。如果事件具有相同的日期,则仅显示一个。我想要的输出是

{
"2018-02-01":[{"comments": "First Event"}, {"comments": "Second Event"}],
"2018-03-01":[{"comments": "Third Event"}]
}

**我需要将具有相同日期的事件组合在一起。有人可以帮助我获得我想要的数据结构吗?我不知道如何解决这个问题。谢谢**

4 个答案:

答案 0 :(得分:1)

您可以使用reduce将对象数组分组到由日期字符串索引的单个对象中。如果输入和输出项目不是一对一的话,则无法使用map



const eventsFetchedFromApi=[{startDateTime:"2018-02-01T11:10:43",comments:"First Event"},{startDateTime:"2018-03-01T11:12:43",comments:"Second Event"},{startDateTime:"2018-02-01T11:18:43",comments:"Third Event"}];
const output = eventsFetchedFromApi.reduce((a, { startDateTime, comments }) => {
  const prop = startDateTime.slice(0, 10);
  if (!a[prop]) a[prop] = [];
  a[prop].push({ comments });
  return a;
}, {});
console.log(output);




答案 1 :(得分:0)

尝试以下方法:

var arr1 = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Second Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Third Event"},
 ];
 var result = {};
 
 arr1.forEach(function(obj){
 var date = obj.startDateTime.slice(0,10); 
  if(!result[date])
    result[date] = [];
   result[date].push({"comments" : obj.comments});
 });
 console.log(result);

答案 2 :(得分:0)

var resultObject = {};
let eventsFetchedFromApi = [{
        startDateTime: "2018-02-01T11:10:43",
        comments: "First Event"
    },
    {
        startDateTime: "2018-03-01T11:12:43",
        comments: "Second Event"
    },
    {
        startDateTime: "2018-02-01T11:18:43",
        comments: "Third Event"
    },
]

eventsFetchedFromApi.forEach((val) => {
    let date = val.startDateTime.split("T")[0];
    resultObject[date] = resultObject[date] || {};
    resultObject[date].startDateTime = val.startDateTime;
    resultObject[date].comments = resultObject[date].comments || []
    resultObject[date].comments.push({
        comments: val.comments
    });
});
const finalResultArray = Object.values(resultObject);
console.log(finalResultArray);

答案 3 :(得分:0)

这个减速机能做你想做的吗?



const dt2Date = dateStr => dateStr.split("T")[0];
const createOrPush = (obj, key, value) => obj[key] 
  ? obj[key].push(value) && obj[key] 
  : [].concat(value);
const reducer = (redo, {startDateTime, comments}) => 
  Object.assign( redo, { [dt2Date(startDateTime)]: 
    createOrPush(redo, dt2Date(startDateTime), {comments: comments}) });

const array2Convert = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Third Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Second Event"},
];

console.log(array2Convert.reduce( reducer, {} ));