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

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

我有以下资料:

currentPhase: {
      endDate: "2030-12-31T09:00:00.000Z"
      startDate: "2021-06-26T11:00:00.000Z"
      status: "ended"
     }

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

phases: {
        ended: { 
          startDate: "2021-06-26T11:00:00.000Z", 
          endDate: "2030-12-31T09:00:00.000Z"},

        openingSoon: { 
          startDate: "1899-12-31T21:00:00.000Z", 
          endDate: "2021-06-17T09:25:31.000Z" },  
  ​​    
        playing: { 
          startDate: "2021-06-15T21:00:00.000Z", 
          endDate: "2021-08-30T21:00:00.000Z" },   
 ​   
        readyToPlay: { 
          startDate: "2021-06-13T11:00:00.000Z", 
          endDate: "2021-06-15T21:00:00.000Z" },  

        registrationsOpen: { 
          startDate: "2021-06-07T19:00:00.000Z", 
          endDate: "2021-06-12T17:00:00.000Z" },  

        resultsPending: { 
          startDate: "2021-08-30T21:00:00.000Z", 
          endDate: "2021-06-26T11:00:00.000Z" },    
        }

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

qacovj5a

qacovj5a1#

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

var index = 0;

function next() {
  index = ++index >= phases.length ? 0 : index;
  console.log(phases[index].status);
}

function prev() {
  index = --index <= 0 ? phases.length - 1 : index;
  console.log(phases[index].status);
}

var phases = [{
  startDate: "2021-06-26T11:00:00.000Z",
  endDate: "2030-12-31T09:00:00.000Z",
  status: "ended"
}, {
  startDate: "1899-12-31T21:00:00.000Z",
  endDate: "2021-06-17T09:25:31.000Z",
  status: "openingSoon"
}, {
  startDate: "2021-06-15T21:00:00.000Z",
  endDate: "2021-08-30T21:00:00.000Z",
  status: "playing"
}, {
  startDate: "2021-06-13T11:00:00.000Z",
  endDate: "2021-06-15T21:00:00.000Z",
  status: "readyToPlay"
}, {
  startDate: "2021-06-07T19:00:00.000Z",
  endDate: "2021-06-12T17:00:00.000Z",
  status: "registrationsOpen"
}, {
  startDate: "2021-08-30T21:00:00.000Z",
  endDate: "2021-06-26T11:00:00.000Z",
  status: "resultsPending"
}];
<button onclick="prev()">Previous</button>
<button onclick="next()">Next</button>

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

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

ie兼容方法:

Object.keys(phases).map(function (key) {
    var oldObject = {}, newObject = {};
    for (var subKey in oldObject) newObject[subKey] = oldObject[subKey];
    newObject.status = key;
    return newObject;
});
b1payxdu

b1payxdu2#

const phasesStatuses = Object.keys(phases); // Get array of phases statuses
const currentPhaseIndex = phasesStatuses.indexOf(currentPhase.status); // get index of current phase

const nextPhaseStatus = phasesStatuses[currentPhaseIndex + 1]; // get next phase status, which will be the key of next phase in phases array
const nextPhase = phasesStatuses[nextPhaseStatus];
dw1jzc5e

dw1jzc5e3#

const currentPhase = {
    endDate: "2030-12-31T09:00:00.000Z",
    startDate: "2021-06-26T11:00:00.000Z",
    status: "ended",
}

const phases = {
    ended: {
        startDate: "2021-06-26T11:00:00.000Z",
        endDate: "2030-12-31T09:00:00.000Z" },
    openingSoon: {
        startDate: "1899-12-31T21:00:00.000Z",
        endDate: "2021-06-17T09:25:31.000Z" },
    playing: {
        startDate: "2021-06-15T21:00:00.000Z",
        endDate: "2021-08-30T21:00:00.000Z" },
    readyToPlay: {
        startDate: "2021-06-13T11:00:00.000Z",
        endDate: "2021-06-15T21:00:00.000Z" },

    registrationsOpen: {
        startDate: "2021-06-07T19:00:00.000Z",
        endDate: "2021-06-12T17:00:00.000Z" },

    resultsPending: {
        startDate: "2021-08-30T21:00:00.000Z",
        endDate: "2021-06-26T11:00:00.000Z" },
}

const phasesList = Object.keys(phases)
const currentPhaseIndex = phasesList.indexOf(currentPhase.status)
const nextPhase = currentPhaseIndex === phasesList.length - 1 ? phasesList[0] : phasesList[currentPhaseIndex + 1]

console.log(nextPhase)

相关问题