我想找到最大的变化

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

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

  1. var olddata =
  2. [
  3. {"token": "NORD", "oldprice": 1},
  4. {"token": "DYP", "oldprice": 2.43},
  5. {"token": "ZIL", "oldprice": 0.20},
  6. {"token": "VET", "oldprice": 6.33}
  7. ]
  8. var newdata =
  9. [
  10. {"token": "NORD", "newprice": 1.20},
  11. {"token": "DYP", "newprice": 2.80},
  12. {"token": "ZIL", "newprice": 0.40},
  13. {"token": "VET", "newprice": 6.90}
  14. ]
  15. function findBiggestChange(oldData, newData) {
  16. var previousDifference = null;
  17. var changeIndex;
  18. oldData.forEach((obj, i) => {
  19. var data = newData.find(d => d.token === obj.token);
  20. var currentDifference = previousDifference ? Math.abs.forEach(data.newprice - obj.oldprice) : data.newprice;
  21. changeIndex = currentDifference > previousDifference ? i : 0;
  22. });
  23. }
  24. 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%
如果您的“最大变化”是按百分比计算的,那么您要查找的代码是:

  1. var olddata =
  2. [
  3. {"token": "NORD", "oldprice": 1},
  4. {"token": "DYP", "oldprice": 2.43},
  5. {"token": "ZIL", "oldprice": 0.20},
  6. {"token": "VET", "oldprice": 6.33}
  7. ];
  8. var newdata =
  9. [
  10. {"token": "NORD", "newprice": 1.20},
  11. {"token": "DYP", "newprice": 2.80},
  12. {"token": "ZIL", "newprice": 0.40},
  13. {"token": "VET", "newprice": 6.90}
  14. ];
  15. function findBiggestChange(oldData, newData) {
  16. var biggestDifference = -Infinity;
  17. var biggestChangeObject = null;
  18. oldData.forEach(obj => {
  19. var data = newData.find(d => d.token === obj.token);
  20. var currentDifference = (data.newprice/obj.oldprice) - 1;
  21. if (currentDifference > biggestDifference) {
  22. biggestDifference = currentDifference;
  23. biggestChangeObject = obj;
  24. }
  25. });
  26. return biggestChangeObject;
  27. }
  28. console.log(findBiggestChange(olddata,newdata));
展开查看全部
6ovsh4lw

6ovsh4lw2#

以下代码给出了zil对象作为结果,因为它具有最大的差异。

  1. var olddata =
  2. [
  3. {"token": "NORD", "oldprice": 1},
  4. {"token": "DYP", "oldprice": 2.43},
  5. {"token": "ZIL", "oldprice": 0.20},
  6. {"token": "VET", "oldprice": 6.33}
  7. ]
  8. var newdata =
  9. [
  10. {"token": "NORD", "newprice": 1.20},
  11. {"token": "DYP", "newprice": 2.80},
  12. {"token": "ZIL", "newprice": 0.40},
  13. {"token": "VET", "newprice": 6.90}
  14. ]
  15. function findBiggestChange(oldData, newData) {
  16. var previousDifference = null;
  17. var changeIndex;
  18. oldData.forEach((obj, i) => {
  19. var data = newData.find(d => d.token === obj.token);
  20. var currentDifference = (data.newprice/obj.oldprice) - (1);
  21. if (!previousDifference) {
  22. previousDifference = currentDifference;
  23. }
  24. if (currentDifference > previousDifference) {
  25. changeIndex = i;
  26. previousDifference = currentDifference;
  27. }
  28. });
  29. return oldData[changeIndex];
  30. }
  31. console.log(findBiggestChange(olddata,newdata));
展开查看全部
ujv3wf0j

ujv3wf0j3#

我们可以使用array.map创建更改列表,包括绝对(更改)和百分比(百分比更改)。
使用array.sort,我们将按照百分比变化的降序进行排序,以获得最大的百分比变化:

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

相关问题