以下是我的代码

 response.data.data.map((item, index) => {
                console.log('response',item);
                const itemIndex = dataList.findIndex(v => v.dt3 === item.dt3);
                if (itemIndex > -1) {
                  //update quantity in list if same item is selected more than one time
                  const value = {
                    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
                  };
                  dataList.push(value);
                } else {
                  const value = {
                    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
                  };
                  dataList.push(item.dt3);
                  for(let i = 0; i<dataList.length; i++){
                    dataList[i] = value;
                  }             
                }
              });

//预期yields

 const DATA = [
    {
    title: '4th April, 2020',
    data: [
      {
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    },{
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    }
     ],
    },
    {
    title: '3rd April, 2020',
    data: [
    {
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    },{
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    }
     ],
    },
    {
    title: '2nd April, 2020',
    data: [
    {
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    },{
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    }
     ],
    },
    {
    title: '1st April, 2020',
    data: [
     {
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    },{
    ds: item.ds,
                    bl: item.bl,
                    mty: item.od_auto.Mty,
                    name: item.customer_name_auto,
    }
     ],
    },
    ];

下面是我的API响应

{
    "status": 1,
    "data": [
        {
            "aid": 188427,
            "s": 1,
            "dt": "19th Oct 2022",
            "dt3": "19-10-2022",
            "ds": "Proceeds",
            "cr": 172,
            "bk": "sdlsjkfhjdfhgf",
            "bkid": 1,
            "up": "",
            "od": {
                "Mty": "",
                "Mre": "",
                "Mct": "",
                "Mci": "",
                "Mdt": "",
                "Mdi": ""
            },
            "typ": "",
            "cnm": "",
            "tm": "19th Oct 2022 19:31",
            "bl": "224",
            "od_auto": {
                "Mty": "Services",
                "Mci": 77,
                "Mct": 2,
                "Mre": "",
                "Mdt": "",
                "Mdi": ""
            },
            "customer_name_auto": null
        },
        {
            "aid": 188426,
            "s": 2,
            "dt": "19th Oct 2022",
            "dt3": "19-10-2022",
            "ds": "cslkdjfhsjkdfhjshfjs",
            "cr": 1.01,
            "bk": "slkdjfhsjkdfhljdfh",
            "bkid": 397,
            "up": "",
            "od": {
                "Mty": "",
                "Mre": "",
                "Mct": "",
                "Mci": "",
                "Mdt": "",
                "Mdi": ""
            },
            "typ": "",
            "cnm": "",
            "tm": "19th Oct 2022 18:07",
            "bl": "2487.22",
            "od_auto": {
                "Mty": "djfdfhghgh",
                "Mci": 181,
                "Mct": 1,
                "Mre": "",
                "Mdt": "",
                "Mdi": ""
            },
            "customer_name_auto": "skdhfshdghsd"
        },
]}

我从Api获取响应以创建倒排扁平列表.在我的响应数组中,数组中的每一项都有Date对象.现在我想将日期相同的项分组到一个数组中.请帮助我排列这些数组项

推荐答案

如果您希望根据Date属性对对象数组进行分组,则可以使用此函数

const data_array  = {
    "status": 1,
    "data": [
        {
            "aid": 188427,
            "s": 1,
            "dt": "19th Oct 2022",
            "dt3": "19-10-2022",
            "ds": "Proceeds",
            "cr": 172,
            "bk": "sdlsjkfhjdfhgf",
            "bkid": 1,
            "up": "",
            "od": {
                "Mty": "",
                "Mre": "",
                "Mct": "",
                "Mci": "",
                "Mdt": "",
                "Mdi": ""
            },
            "typ": "",
            "cnm": "",
            "tm": "19th Oct 2022 19:31",
            "bl": "224",
            "od_auto": {
                "Mty": "Services",
                "Mci": 77,
                "Mct": 2,
                "Mre": "",
                "Mdt": "",
                "Mdi": ""
            },
            "customer_name_auto": null
        },
        {
            "aid": 188426,
            "s": 2,
            "dt": "19th Oct 2022",
            "dt3": "19-10-2022",
            "ds": "cslkdjfhsjkdfhjshfjs",
            "cr": 1.01,
            "bk": "slkdjfhsjkdfhljdfh",
            "bkid": 397,
            "up": "",
            "od": {
                "Mty": "",
                "Mre": "",
                "Mct": "",
                "Mci": "",
                "Mdt": "",
                "Mdi": ""
            },
            "typ": "",
            "cnm": "",
            "tm": "19th Oct 2022 18:07",
            "bl": "2487.22",
            "od_auto": {
                "Mty": "djfdfhghgh",
                "Mci": 181,
                "Mct": 1,
                "Mre": "",
                "Mdt": "",
                "Mdi": ""
            },
            "customer_name_auto": "skdhfshdghsd"
        },
]}


var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};

console.log(groupBy(data_array.data, 'dt3'));

Let data_group_by_date=groupBy(data_array.data,‘DT3’) 您可以使用任何对象属性对数组进行分组

您可以使用此代码对列表数据进行分区

Const sections_list_data=[]; 对象键(DATA_GROUP_BY_DATE).map((Item,i)=>;{ Sections.Push({ 标题:项目, 数据:Data_Group_by_Date[项目], }); });

React-native相关问答推荐

react 本机 TextInput 将其他元素移出屏幕

React Native 映射未定义不是函数

React Native - 何时调用componentWillUnmount?

React Native Navigation const { navigate } = this.props.navigation;

找不到变量 atob

react-native-firebase crashlytics 未显示在 firebase 仪表板上

react-native: 'adb' 不是内部或外部命令、可运行程序或批处理文件

submit提交后如何让 React Native TextInput 保持焦点?

如何在 react-native 中在子级和父级之间传递数据?

可以在没有 await 关键字的情况下调用异步函数吗?

React Native:fetch request failed with error - TypeError: Network request failed(…)

FlatList contentContainerStyle -> justifyContent: 'center' 导致滚动问题

Typescript:onPress 类型

java.lang.UnsatisfiedLinkError:找不到要加载的 DSO:libfbjni.so 结果:0

组件div的视图配置 getter 回调必须是一个函数(收到 undefined)

通过 IOS 中的 Linking.openURL React Native Open 设置

使用 React Navigation 导航堆栈时重新渲染组件

如何在 react-native 中为在后台杀死的 android 应用程序保持状态

键盘可见时无法单击按钮

如何在react-native中将文本放置在图像上?