将数据数组转换为 SectionList 组件的分组数据

Transform array of data into grouped data for SectionList component

我承认 Javascript 不是我最擅长的语言,而且 React Native 是非常新的,所以,可能有一个我没有看到的明显简单的方法来做到这一点。

我有一个 API 以简单的结构呈现一些交易数据:

[
  {
    "id": 1,
    "title": "Apple Store",
    "date": "2021-09-10",
    "amount": "0.00",
  },
  {
    "id": 41,
    "title": "Zulauf, Walter and Metz",
    "date": "2021-09-10",
    "amount": ".00",
  },
  {
    "id": 9,
    "title": "Aufderhar PLC",
    "date": "2021-09-09",
    "amount": ".00",
  },
  {
    "id": 10,
    "title": "Bayer and Sons",
    "date": "2021-09-07",
    "amount": ".00",
  }
]

我想使用 SectionList 组件显示此数据,其中交易按日期分段。我(可能是粗略的)解决这个问题的尝试是将这些数据转换为以下结构:

[
  {
    "date": "2021-09-10",
    "transactions": [
      {
        "id": 1,
        "title": "Apple Store",
        "date": "2021-09-10",
        "amount": "0.00",
      },
      {
        "id": 41,
        "title": "Zulauf, Walter and Metz",
        "date": "2021-09-10",
        "amount": ".00",
      }
    ]
  },
  {
    "date": "2021-09-09",
    "transactions": [
      {
        "id": 9,
        "title": "Aufderhar PLC",
        "date": "2021-09-09",
        "amount": ".00",
      }
    ]
  },
  {
    "date": "2021-09-07",
    "transactions": [
      {
        "id": 10,
        "title": "Bayer and Sons",
        "date": "2021-09-07",
        "amount": ".00",
      }
    ]
  }
]

但老实说,我不知道如何转换这些数据(或者是否有更好的方法来解决这个问题)。我开始使用 Lodash 的 groupBy 函数,这看起来很有前途,但看起来 SectionList 不需要 object,它需要一个数组。

直接将 groupBy 的输出转换为数组会丢弃键,我得到了分组数据,但 header.

部分没有明确的值

同样,可能有一些非常简单的方法可以解决这个问题,数据始终以平面数组的形式出现。我感谢任何人可以向我指出的任何指导、帮助或示例。

lodash groupBy 只是帮助您处理分组数据,您应该通过将分组数据转换成您的格式来处理分组数据。

const input = [
  {
    "id": 1,
    "title": "Apple Store",
    "date": "2021-09-10",
    "amount": "0.00",
  },
  {
    "id": 41,
    "title": "Zulauf, Walter and Metz",
    "date": "2021-09-10",
    "amount": ".00",
  },
  {
    "id": 9,
    "title": "Aufderhar PLC",
    "date": "2021-09-09",
    "amount": ".00",
  },
  {
    "id": 10,
    "title": "Bayer and Sons",
    "date": "2021-09-07",
    "amount": ".00",
  }
];

const groupedArray = _.groupBy(input, "date");

let result = [];
for (const [key, value] of Object.entries(groupedArray)) {
   result.push({
     'date': key,
     'transactions': value
   })
}

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

const input = [
  {
    "id": 1,
    "title": "Apple Store",
    "date": "2021-09-10",
    "amount": "0.00",
  },
  {
    "id": 41,
    "title": "Zulauf, Walter and Metz",
    "date": "2021-09-10",
    "amount": ".00",
  },
  {
    "id": 9,
    "title": "Aufderhar PLC",
    "date": "2021-09-09",
    "amount": ".00",
  },
  {
    "id": 10,
    "title": "Bayer and Sons",
    "date": "2021-09-07",
    "amount": ".00",
  }
]

const result = input.reduce((accum, current)=> {
  let dateGroup = accum.find(x => x.date === current.date);
  if(!dateGroup) {
    dateGroup = { date: current.date, transactions: [] }
    accum.push(dateGroup);
  }
  dateGroup.transactions.push(current);
  return accum;
}, []);

console.log(result)

给定一个数组,只要您的结果期望具有相同数量的元素,请使用 map,但由于您的结果具有不同数量的元素,请使用 reduce,如上所示。这个想法是让reduce循环遍历每个元素,看看是否可以找到该元素,然后将当前元素推入列表

简直

const data = 
  [ { id:  1, title: 'Apple Store',             date: '2021-09-10', amount: '0.00' } 
  , { id: 41, title: 'Zulauf, Walter and Metz', date: '2021-09-10', amount:  '.00' } 
  , { id:  9, title: 'Aufderhar PLC',           date: '2021-09-09', amount:  '.00' } 
  , { id: 10, title: 'Bayer and Sons',          date: '2021-09-07', amount:  '.00' } 
  ] 

const res = Object.entries(data.reduce((r,{id,title,date,amount})=>
  {
  r[date] = r[date] ?? []
  r[date].push({id,title,date,amount})
  return r
  },{})).map(([k,v])=>({date:k,transactions:v}))

console.log( res )
.as-console-wrapper { max-height: 100% !important; top: 0 }

使用 lodash,您可以按 date 分组,然后映射到所需的形式:

const input = [{"id":1,"title":"Apple Store","date":"2021-09-10","amount":"0.00"},{"id":41,"title":"Zulauf, Walter and Metz","date":"2021-09-10","amount":".00"},{"id":9,"title":"Aufderhar PLC","date":"2021-09-09","amount":".00"},{"id":10,"title":"Bayer and Sons","date":"2021-09-07","amount":".00"}];

const result = _.map(
  _.groupBy(input, 'date'),
  (transactions, date) => ({ date, transactions })
)

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

你可以使用 lodash

var result = _(data)
    .groupBy(item => item.date)
    .map((value, key) => ({date: key, transactions: value}))
    .value();