javascript - 如何在 React Native 中对组件数组进行排序?

标签 javascript arrays reactjs react-native

我有一个嵌套映射来遍历我的数据,然后为我的数据中的每个对象返回一个 react 组件。检查下面的代码:

return props.dataArray.map((classObj) => {
                    return classObj.timetable.map((timetableObj) => {
                        if (timetableObj.weekday === props.day) {
                            return ttCard({
                                lecturer: classObj.lecturer,
                                module: classObj.module,
                                room: classObj.room,
                                class_id: classObj.id,
                                timetable_id: timetableObj.id,
                                startTime: timetableObj.start_time,
                                endTime: timetableObj.end_time,
                                weekday: timetableObj.weekday
                            });
                        }
                        return null;
                    });
                });

我的问题是我的组件没有排序,它们必须排序,因为这是学生的时间表。查看下图,注意 10:30 AM 是我的选项卡 View 中的最新卡片:

enter image description here

到目前为止我尝试过的事情:

1- 创建一个新数组,然后在 if 语句 block 之后对其进行排序 - 不起作用 2- 将排序数组返回给函数并映射该数组以返回组件 - 不起作用 3- 在组件参数中循环并将对象作为函数返回:

ttCard( sortedArr.forEach((ttObject) => {
return ttObject
}))

也没有工作

我用谷歌搜索并尝试进行研究,但到目前为止没有希望。我知道最初我的嵌套 map 很糟糕,但这是我的对象的样子:

[
    {
        "class_code": "English-5-G-2-L-1911-test-teacher",
        "id": 23,
        "lecturer": "test teacher",
        "module": "English-5",
        "room": "L",
        "timetable": [
            {
                "end_time": "9:00",
                "id": 37,
                "start_time": "8:00",
                "weekday": "Sunday"
            },
            {
                "end_time": "10:00",
                "id": 38,
                "start_time": "9:00",
                "weekday": "Sunday"
            },
            {
                "end_time": "10:00",
                "id": 47,
                "start_time": "9:00",
                "weekday": "Monday"
            },
            {
                "end_time": "10:00",
                "id": 48,
                "start_time": "9:00",
                "weekday": "Tuesday"
            },
            {
                "end_time": "11:30",
                "id": 52,
                "start_time": "10:30",
                "weekday": "Tuesday"
            },
            {
                "end_time": "11:30",
                "id": 54,
                "start_time": "10:30",
                "weekday": "Thursday"
            },
            {
                "end_time": "12:30",
                "id": 58,
                "start_time": "11:30",
                "weekday": "Thursday"
            },
            {
                "end_time": "14:00",
                "id": 61,
                "start_time": "13:00",
                "weekday": "Wednesday"
            }
        ]
    }
]

最佳答案

您可以在输出中使用 sort 来按 startTime 排列数组

.sort((a, b) => {
    // These lines are so we can directly compare the times
    // I would recommend to send timestamps down with your data (if possible)
    var aTime = a.startTime.replace(':', '');
    var bTime = b.startTime.replace(':', '');

    // Sort by smallest first
    return aTime - bTime;
  });

我还建议过滤掉数组中的空值

用你的例子 -

var classesArr = props.dataArray.map((classObj) => {
  return classObj.timetable.map((timetableObj) => {
    if (timetableObj.weekday === props.day) {
      return {
        lecturer: classObj.lecturer,
        module: classObj.module,
        room: classObj.room,
        class_id: classObj.id,
        timetable_id: timetableObj.id,
        startTime: timetableObj.start_time,
        endTime: timetableObj.end_time,
        weekday: timetableObj.weekday
      }
    }
    return null;
  }).filter(x => x); // Filter out any null values
}).filter(x => x); // Filter out any null values.

// Flatten array so it is sortable/mapable
return [].concat.apply([], classesArr).sort((a, b) => {
  // These lines are so we can directly compare the times
  // I would recommend to send timestamps down with your data (if possible)
  var aTime = a.startTime.replace(':', '');
  var bTime = b.startTime.replace(':', '');
  // Sort by smallest time first
  return aTime - bTime;
}).map((x) => {
  return ttCard(x);
})

关于javascript - 如何在 React Native 中对组件数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428784/

相关文章:

reactjs - 如何在 react Electron 应用程序中使用 cookie?

javascript - 无法读取React中未定义的属性 'forEach'

javascript - window.onpaint 是什么?

javascript - 打印数组中的键和值

javascript - 更改事件对 selectfield 不起作用?

mySQL 查询中的 PHP 数组 : "Unknown column"

javascript - 动态添加和删除iframe : Google red flag?

c - 从文件中读取输入并以 C 中的特定格式存储在数组中

arrays - 在JSON模式中将数组定义为非空

javascript - 将空数组插入react状态