【问题标题】:How to sort an array of components in React Native?如何在 React Native 中对一组组件进行排序?
【发布时间】:2020-04-13 04:13:26
【问题描述】:

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

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 是我标签视图中的最新卡片:

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

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

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

也不行

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

[
    {
        "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"
            }
        ]
    }
]

【问题讨论】:

  • 你可以在父组件中对它们进行排序,因为它们是道具吗?如果没有,您会考虑在构造函数中对它们进行排序并将排序结果存储在this.state 中吗?因为每次渲染都对它们进行排序对我来说似乎对性能非常不利
  • 排序和地图将按预期工作,.. 问题是您没有显示此代码,因此很难建议您是否出错了。此外,在对时间进行排序时,请考虑 0 填充时间,因为 9:00 在字符串排序中位于 11:20 之后。但 09:00 不是。

标签: javascript arrays reactjs react-native


【解决方案1】:

您可以在输出中使用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);
})

【讨论】:

  • 嘿,我试过你的答案,但它不起作用,我把它解析成一个 int 但仍然。还有其他解决方案吗?
  • @ZyzzShembesh 我的错,我现在更新了排序以使用return aTime - bTime; 用你的数组测试过,它可以工作:)
  • 嘿,再次感谢您的帮助,我再次尝试了它仍然无法正常工作:(。您看到我的数据在上面的样子了吗?
  • 你能解释一下“不工作”是什么意思吗?这是一个显示排序工作的 JSFiddle jsfiddle.net/phmq230r
  • 基本上我的清单是一样的。 10:30仍然是最后一张牌。抱歉回复晚了,但我的 ISP 有问题。这个 Js fiddle 包含我的整个数据列表。您可以尝试在时间表中显示仅在周日进行并排序的课程吗? jsfiddle.net/oymz8h4w
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 2018-09-19
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多