javascript-基于特定对象选择对象数组中的下一个对象

cetgtptt  于 2021-09-29  发布在  Java
关注(0)|答案(3)|浏览(481)

我有以下资料:

  1. currentPhase: {
  2. endDate: "2030-12-31T09:00:00.000Z"
  3. startDate: "2021-06-26T11:00:00.000Z"
  4. status: "ended"
  5. }

以及具有更多对象的对象:

  1. phases: {
  2. ended: {
  3. startDate: "2021-06-26T11:00:00.000Z",
  4. endDate: "2030-12-31T09:00:00.000Z"},
  5. openingSoon: {
  6. startDate: "1899-12-31T21:00:00.000Z",
  7. endDate: "2021-06-17T09:25:31.000Z" },
  8. ​​
  9. playing: {
  10. startDate: "2021-06-15T21:00:00.000Z",
  11. endDate: "2021-08-30T21:00:00.000Z" },
  12. readyToPlay: {
  13. startDate: "2021-06-13T11:00:00.000Z",
  14. endDate: "2021-06-15T21:00:00.000Z" },
  15. registrationsOpen: {
  16. startDate: "2021-06-07T19:00:00.000Z",
  17. endDate: "2021-06-12T17:00:00.000Z" },
  18. resultsPending: {
  19. startDate: "2021-08-30T21:00:00.000Z",
  20. endDate: "2021-06-26T11:00:00.000Z" },
  21. }

currentphase会经常更改,每当currentphase更改时,我需要根据currentphase从phase中选择下一个phase。
我尝试的是根据currentphase的enddate筛选Phase(因为它不是数组,所以不起作用),我还可以做的是Phase[currentphase.status],但是如何检索当前的阶段索引[currentphase.status],以便选择阶段[currentphase.status]之后的下一个对象,以便显示其开始日期?
提前谢谢!

qacovj5a

qacovj5a1#

可以将对象放在数组中,只需将当前索引存储在另一个变量中。我还添加了made,这样它就可以循环,如果您需要的话,只需删除ternaries并使用 Math.maxMath.min 相反

  1. var index = 0;
  2. function next() {
  3. index = ++index >= phases.length ? 0 : index;
  4. console.log(phases[index].status);
  5. }
  6. function prev() {
  7. index = --index <= 0 ? phases.length - 1 : index;
  8. console.log(phases[index].status);
  9. }
  10. var phases = [{
  11. startDate: "2021-06-26T11:00:00.000Z",
  12. endDate: "2030-12-31T09:00:00.000Z",
  13. status: "ended"
  14. }, {
  15. startDate: "1899-12-31T21:00:00.000Z",
  16. endDate: "2021-06-17T09:25:31.000Z",
  17. status: "openingSoon"
  18. }, {
  19. startDate: "2021-06-15T21:00:00.000Z",
  20. endDate: "2021-08-30T21:00:00.000Z",
  21. status: "playing"
  22. }, {
  23. startDate: "2021-06-13T11:00:00.000Z",
  24. endDate: "2021-06-15T21:00:00.000Z",
  25. status: "readyToPlay"
  26. }, {
  27. startDate: "2021-06-07T19:00:00.000Z",
  28. endDate: "2021-06-12T17:00:00.000Z",
  29. status: "registrationsOpen"
  30. }, {
  31. startDate: "2021-08-30T21:00:00.000Z",
  32. endDate: "2021-06-26T11:00:00.000Z",
  33. status: "resultsPending"
  34. }];
  1. <button onclick="prev()">Previous</button>
  2. <button onclick="next()">Next</button>

编辑fast es6将原始对象转换为阵列的方法:

  1. Object.entries(phases).map(([key, value]) => ({ ...value, status: key }));

ie兼容方法:

  1. Object.keys(phases).map(function (key) {
  2. var oldObject = {}, newObject = {};
  3. for (var subKey in oldObject) newObject[subKey] = oldObject[subKey];
  4. newObject.status = key;
  5. return newObject;
  6. });
展开查看全部
b1payxdu

b1payxdu2#

  1. const phasesStatuses = Object.keys(phases); // Get array of phases statuses
  2. const currentPhaseIndex = phasesStatuses.indexOf(currentPhase.status); // get index of current phase
  3. const nextPhaseStatus = phasesStatuses[currentPhaseIndex + 1]; // get next phase status, which will be the key of next phase in phases array
  4. const nextPhase = phasesStatuses[nextPhaseStatus];
dw1jzc5e

dw1jzc5e3#

  1. const currentPhase = {
  2. endDate: "2030-12-31T09:00:00.000Z",
  3. startDate: "2021-06-26T11:00:00.000Z",
  4. status: "ended",
  5. }
  6. const phases = {
  7. ended: {
  8. startDate: "2021-06-26T11:00:00.000Z",
  9. endDate: "2030-12-31T09:00:00.000Z" },
  10. openingSoon: {
  11. startDate: "1899-12-31T21:00:00.000Z",
  12. endDate: "2021-06-17T09:25:31.000Z" },
  13. playing: {
  14. startDate: "2021-06-15T21:00:00.000Z",
  15. endDate: "2021-08-30T21:00:00.000Z" },
  16. readyToPlay: {
  17. startDate: "2021-06-13T11:00:00.000Z",
  18. endDate: "2021-06-15T21:00:00.000Z" },
  19. registrationsOpen: {
  20. startDate: "2021-06-07T19:00:00.000Z",
  21. endDate: "2021-06-12T17:00:00.000Z" },
  22. resultsPending: {
  23. startDate: "2021-08-30T21:00:00.000Z",
  24. endDate: "2021-06-26T11:00:00.000Z" },
  25. }
  26. const phasesList = Object.keys(phases)
  27. const currentPhaseIndex = phasesList.indexOf(currentPhase.status)
  28. const nextPhase = currentPhaseIndex === phasesList.length - 1 ? phasesList[0] : phasesList[currentPhaseIndex + 1]
  29. console.log(nextPhase)
展开查看全部

相关问题