【问题标题】:Split the time & push object to array based on the consultation time - Javascript根据咨询时间拆分时间并将对象推送到数组 - Javascript
【发布时间】:2021-01-27 14:36:19
【问题描述】:

我有这样的对象数组(假设是大数组)

[
  {
    userID: '25634076cc1fccb0978ciug9f8f8b59cf7e6e0b0615d00de4df7e233bdb0d399',
    consultationTime: 4200 // in seconds
  },
  {
    userID: '48e69555d778f9b9a3a1d553b9c3b8f7dd6a3394ac82df1433b60a69c055d23d',
    consultationTime: 198741 // in seconds
  },
  {
    userID: 'a75d2f0a583b29ddba803af2a02ae1ee3b7b2c112fff8f41707aa9bb9aac9c90',
    consultationTime: 10 // in seconds
  }
];

要求: 我需要将咨询时间转换为分钟。然后,如果consultationTime 少于20 分钟,那么我需要将带有代码“11”的consultationTime 推送到billingDetails。如果consultationTime大于20分钟,则需要拆分剩余时间并将时间推送到编码“22”为对象数组。剩余时间属性将通过与总时间相减来计算。

示例:对于 4200 秒(70 分钟),前 20 分钟转到代码 11,其余 50 分钟转到代码 22,因为对象数组包含第一个对象包含 20,接下来是 20,接下来是输出中的 10。

预期输出:

[
{
userID: '25634076cc1fccb0978ciug9f8f8b59cf7e6e0b0615d00de4df7e233bdb0d399',
billingDetails: [{
code: 11,
totalTime: 20 // first 20 minutes
remainingTime: 0 // 20 mins - totalTime
},
{
code: 22,
totalTime: 20
remainingTime: 0
},
{
code: 22,
totalTime: 20 
remainingTime: 0
},
{
code: 22,
totalTime: 10
remainingTime: 10 (20 minutes - 10 minutes)
}
]
},
{... similarly for other users},
];

我很难得出相同的逻辑。因此,我们将不胜感激。

【问题讨论】:

  • 如果您指定反对投票的原因会更好。
  • 不是我的,但我怀疑因为你根本没有表现出解决这个问题的努力 - 本质上 SO 不是代码编写服务。
  • @Jamiec 实际上,如果有人能指出逻辑然后我可以尝试,但坚持如何开始。
  • 如何将秒转换为分钟 - 我相信你可以做到这一点。关于将一个数组映射到另一个数组(提示!),您还尝试/研究过什么?
  • @Jamiec 是的,将秒转换为分钟我可以做到。我被卡住的地方不知道如何将时间分成 20 分钟。

标签: javascript node.js arrays ecmascript-6


【解决方案1】:

您可以循环遍历时间并从consultationTime 中删除一个“时间段”,直到没有剩余时间为止。然后使用map(),您可以为最终结果创建一个新对象。

const data = [
  {
    userID: '25634076cc1fccb0978ciug9f8f8b59cf7e6e0b0615d00de4df7e233bdb0d399',
    consultationTime: 4200 // in seconds
  },
  {
    userID: '48e69555d778f9b9a3a1d553b9c3b8f7dd6a3394ac82df1433b60a69c055d23d',
    consultationTime: 198741 // in seconds
  },
  {
    userID: 'a75d2f0a583b29ddba803af2a02ae1ee3b7b2c112fff8f41707aa9bb9aac9c90',
    consultationTime: 10 // in seconds
  }
];

const getBillingDetails = (seconds, size) => {
  let minutes = seconds / 60;
  let first = true;
  const details = [];
  
  while(minutes > 0) {
    const haveEnough = minutes >= size;
    
    details.push({
      code: first ? 11 : 22,
      totalTime: haveEnough ? size : minutes,
      remainingTime: haveEnough ? 0 : size - minutes,
    })
    
    minutes -= size;
    first = false;
  }
  
  return details;
}

const consultationTimeToBillingDetails = (data, size) => {
  return data.map(({consultationTime, ...rest}) => {
    return {
      ...rest,
      billingDetails: getBillingDetails(consultationTime, size)
    }
  });
}

const result = consultationTimeToBillingDetails(data, 20);

console.log(result);
.as-console-wrapper { max-height: 100% !important; }

说明

const data = [
  {
    userID: '25634076cc1fccb0978ciug9f8f8b59cf7e6e0b0615d00de4df7e233bdb0d399',
    consultationTime: 4200 // in seconds
  },
  {
    userID: '48e69555d778f9b9a3a1d553b9c3b8f7dd6a3394ac82df1433b60a69c055d23d',
    consultationTime: 198741 // in seconds
  },
  {
    userID: 'a75d2f0a583b29ddba803af2a02ae1ee3b7b2c112fff8f41707aa9bb9aac9c90',
    consultationTime: 10 // in seconds
  }
];

const getBillingDetails = (seconds, size) => {
  // Turn seconds into minutes
  let minutes = seconds / 60;
  // Boolean to check if it is the first iteration since it has a different code
  let first = true;
  // Array to push objects into
  const details = [];
  
  // If we still have minutes left, execute the while block
  while(minutes > 0) {
    // Boolean to check if we have atleast 20 minutes left
    const haveEnough = minutes >= size;
    
    details.push({
      code: first ? 11 : 22, // Set code to 11 on first iteration all the others have code 22
      totalTime: haveEnough ? size : minutes, // If we have atleast 20 minutes set that as totalTime else set it as the remaining minutes
      remainingTime: haveEnough ? 0 : size - minutes, // If we have atleast 20 minutes set the remaingTime to 0 else calculate the missing time.
    })
    
    // Remove 20 minutes before we start the while loop again
    minutes -= size;
    // Set boolean to false since we finished our first iteration
    first = false;
  }
  
  // Return all the details once the while loop is over
  return details;
}

const consultationTimeToBillingDetails = (data, size) => {
  // Return mapped data
  return data.map(({consultationTime, ...rest}) => {
    return {
      // Spread other properties since we need them on the new object
      ...rest,
      // Get billing details from consultationTime and size
      billingDetails: getBillingDetails(consultationTime, size)
    }
  });
}

// Get billing details from data and specify size to split by
const result = consultationTimeToBillingDetails(data, 20);

console.log(result);
.as-console-wrapper { max-height: 100% !important; }

【讨论】:

  • 谢谢。你能解释一下 const haveEnough = minutes >= size;有点?
  • 它只会解析为布尔值,我们将 20 作为我们想要的分钟数(大小)传递。然后我们检查是否还有 20 分钟或更长时间。如果我们这样做,则变量变为真,否则变为假(您可以通过记录变量来检查这一点)。我将它用于我的ternary operators 几行。 (三元只是一个较小的 if else 语句)
  • @DEEPESHKUMARR 我添加了一个带有一些 cmets 的额外 sn-p 来解释编码的工作原理。 PS:如果可行,我将不胜感激并为正确答案打勾
【解决方案2】:

const inputArray = [
  {
    userID: "25634076cc1fccb0978ciug9f8f8b59cf7e6e0b0615d00de4df7e233bdb0d399",
    consultationTime: 4200,
  },
  {
    userID: "48e69555d778f9b9a3a1d553b9c3b8f7dd6a3394ac82df1433b60a69c055d23d",
    consultationTime: 198741,
  },
  {
    userID: "a75d2f0a583b29ddba803af2a02ae1ee3b7b2c112fff8f41707aa9bb9aac9c90",
    consultationTime: 10,
  },
];

function splitTimeAndPush(inputArray) {
  let outputArray = [];
  inputArray.forEach((element) => {
    let output = { userId: element.userID, billingDetails: [] };
    let minutes = element.consultationTime / 60; // consultaionTime in minutes
    for (let i = 0; i < minutes; i += 20) {
      let bD = { code: "", totalTime: "", remainingTime: "" };
      if (i === 0) {
        bD.code = 11;
      } else {
        bD.code = 22;
      }
      bD.totalTime = minutes - i > 20 ? 20 : minutes - i;
      bD.remainingTime = 20 - bD.totalTime;
      output.billingDetails.push(bD);
    }
    outputArray.push(output);
  });
  return outputArray;
}

const sp = splitTimeAndPush(inputArray);
console.log(sp);

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2021-10-25
    • 2015-08-09
    • 2019-10-08
    • 2021-05-20
    • 2022-01-10
    • 1970-01-01
    • 2018-04-20
    • 2021-11-13
    相关资源
    最近更新 更多