【问题标题】:I want to find Biggest Change我想找到最大的变化
【发布时间】:2021-09-19 11:40:04
【问题描述】:

我的脚本不能只打印数据中的最后一个标记, 我的脚本应该打印 zil,因为它是我数据中最大的变化

var olddata = 
[
    {"token": "NORD", "oldprice": 1},
    {"token": "DYP", "oldprice": 2.43},
    {"token": "ZIL", "oldprice": 0.20},
    {"token": "VET", "oldprice": 6.33}
]

var newdata = 
[
    {"token": "NORD", "newprice": 1.20},
    {"token": "DYP", "newprice": 2.80},
    {"token": "ZIL", "newprice": 0.40},
    {"token": "VET", "newprice": 6.90}
]
function findBiggestChange(oldData, newData) {
  var previousDifference = null;
  var changeIndex;
  oldData.forEach((obj, i) => {
    var data = newData.find(d => d.token === obj.token);
        var currentDifference = previousDifference ? Math.abs.forEach(data.newprice - obj.oldprice) : data.newprice;
        changeIndex = currentDifference > previousDifference ? i : 0;
  });
}

findBiggestChange(olddata,newdata);

【问题讨论】:

    标签: javascript node.js arrays math foreach


    【解决方案1】:

    我们可以使用Array.map 创建一个变化列表,包括绝对(变化)和百分比(百分比变化)。

    使用Array.sort,然后我们将按百分比变化的降序排序以获得最大的百分比变化:

    const olddata = [ {"token": "NORD", "oldprice": 1}, {"token": "DYP", "oldprice": 2.43}, {"token": "ZIL", "oldprice": 0.20}, {"token": "VET", "oldprice": 6.33} ]
    const newdata = [ {"token": "NORD", "newprice": 1.20}, {"token": "DYP", "newprice": 2.80}, {"token": "ZIL", "newprice": 0.40}, {"token": "VET", "newprice": 6.90} ] 
    
    // Get the percentage change and absolute change of each value...
    const changes = olddata.map(({token, oldprice}, idx) => {
        const change = (newdata[idx].newprice - oldprice);
        const percentageChange = 100 * change / oldprice;
        return { token, percentageChange, change };
    });
    
    // Sort by percentage change, in descending order
    const sortedByPercentChange = changes.sort((a, b) => b.percentageChange - a.percentageChange);
    
    console.log("All changes (sorted on % change):", sortedByPercentChange)
    
    // The largest change will be the first element...
    console.log("Largest % change:", sortedByPercentChange[0])
       

    【讨论】:

      【解决方案2】:

      以下代码为您提供 zil 对象作为结果,因为它具有最大的差异。

      var olddata = 
      [
          {"token": "NORD", "oldprice": 1},
          {"token": "DYP", "oldprice": 2.43},
          {"token": "ZIL", "oldprice": 0.20},
          {"token": "VET", "oldprice": 6.33}
      ]
      
      var newdata = 
      [
          {"token": "NORD", "newprice": 1.20},
          {"token": "DYP", "newprice": 2.80},
          {"token": "ZIL", "newprice": 0.40},
          {"token": "VET", "newprice": 6.90}
      ]
      function findBiggestChange(oldData, newData) {
        var previousDifference = null;
        var changeIndex;
        oldData.forEach((obj, i) => {
          var data = newData.find(d => d.token === obj.token);
          var currentDifference = (data.newprice/obj.oldprice) - (1);
          if (!previousDifference) {
              previousDifference = currentDifference;
          }  
          if (currentDifference > previousDifference) {
              changeIndex = i;
              previousDifference = currentDifference;
          }
        });
        return oldData[changeIndex];
      }
      
      console.log(findBiggestChange(olddata,newdata));

      【讨论】:

        【解决方案3】:

        如果您的意思是数量上的“最大变化”,那么您的程序是正确的。因为:

        ZIL:0.40 - 0.20 = 0.20

        VET:6.90 - 6.33 = 0.57(VET 增幅在数量上更高)

        如果您的“最大变化”是按百分比计算的,那么您需要更改检查百分比变化的方式,而不是数量方面的变化。在这种情况下,最大的变化是……:

        ZIL:0.40/0.20 = 2 => 2 - 1 = 1 = 100%(ZIL百分比增加更高)

        兽医:6.90/6.33 = 1.09 => 1.09 - 1 = 0.09 = 9% 兽医:6.90/6.33 = 1.09 => 1.09 - 1 = 0.09 = 9%

        如果您的“最大变化”是百分比,您要查找的代码是:

        var olddata = 
        [
            {"token": "NORD", "oldprice": 1},
            {"token": "DYP", "oldprice": 2.43},
            {"token": "ZIL", "oldprice": 0.20},
            {"token": "VET", "oldprice": 6.33}
        ];
        
        var newdata = 
        [
            {"token": "NORD", "newprice": 1.20},
            {"token": "DYP", "newprice": 2.80},
            {"token": "ZIL", "newprice": 0.40},
            {"token": "VET", "newprice": 6.90}
        ];
        
        function findBiggestChange(oldData, newData) {
          var biggestDifference = -Infinity;
          var biggestChangeObject = null;
          oldData.forEach(obj => {
            var data = newData.find(d => d.token === obj.token);
            var currentDifference = (data.newprice/obj.oldprice) - 1;
            
            if (currentDifference > biggestDifference) {
                biggestDifference = currentDifference;
                biggestChangeObject = obj;
            }
          });
          return biggestChangeObject;
        }
        
        console.log(findBiggestChange(olddata,newdata));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-24
          • 2020-03-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多