我想找到最大的变化

7gs2gvoe  于 2021-09-23  发布在  Java
关注(0)|答案(3)|浏览(266)

我的脚本不能只打印数据中的最后一个标记,我的脚本应该打印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);
0vvn1miw

0vvn1miw1#

如果你的意思是数量上的“最大变化”,那么你的程序是正确的。因为:
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));
6ovsh4lw

6ovsh4lw2#

以下代码给出了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));
ujv3wf0j

ujv3wf0j3#

我们可以使用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])

相关问题