我想找到最大的变化

问题描述

我的脚本不能只打印数据中的最后一个标记, 我的脚本应该打印 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},"newprice": 2.80},"newprice": 0.40},"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);

解决方法

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

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},"newprice": 2.80},"newprice": 0.40},"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));

,

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

var olddata = 
[
    {"token": "NORD","oldprice": 6.33}
]

var newdata = 
[
    {"token": "NORD","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));

,

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

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

const olddata = [ {"token": "NORD","oldprice": 6.33} ]
const newdata = [ {"token": "NORD","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])