【问题标题】:Store Calculations on MongooseDB?在 MongoDB 上存储计算?
【发布时间】:2020-10-24 21:56:36
【问题描述】:

我正在使用 MERN 堆栈。我正在开发一个应用程序,用户输入例如 4 个数字,我应该存储 4 个数字的组合并将它们存储在数据库中而不向用户显示。我遇到的问题是如何存储我在数据库上计算的组合,我不知道从哪里开始。有人知道吗?

【问题讨论】:

    标签: node.js reactjs express mongoose


    【解决方案1】:

    你可以使用这个技巧

    let array = [45, 23, 13, 67];
    let results = [];
    
    // Since you only want pairs, there's no reason
    // to iterate over the last element directly
    for (let i = 0; i < array.length - 1; i++) {
      // This is where you'll capture that last value
      for (let j = i + 1; j < array.length; j++) {
        results.push(`${array[i]} ${array[j]}`);
      }
    }
    console.log(results); // ["45 23", "45 13", "45 67", "23 13", "23 67", "13 67"]
    

    您可以将数组存储为 4 个数字的组合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      相关资源
      最近更新 更多