获取当前对象中的先前对象的值

时间:2018-12-02 05:07:53

标签: javascript node.js

我正在使用一个包含大约 65k数据的数据集。我多次映射到数据集以按摩数据集。在获得了所需格式的数据集之后,我正在使用map对当前商品的价格进行一些计算。但是,每当我返回当前对象时,它都包含前一个对象的计算数据。

每当我记录数据时,它总是显示当前对象以及基于当前对象的计算。但是,返回的对象包含先前对象的数据。这是路线:

const {priceBands} = require('../utils/profitComputations');

let profitArray = [];

//calculating the price bands
profitArray = _.map(nfoArray, item=>{
     console.log(item.cmp);
     //returns the current market price; getting the correct value here

     let priceBandVar = priceBands(Number(item.cmp));
     console.log(priceBandVar);
     //applying some algorithms; getting the correct value here

     return {item: item.cmp, profitBand: priceBandVar};
     //Here I find a mismatch between the parameter and the calculations

});

这是我的'utils / profitComputations'中的priceBands函数:

const _ = require('lodash');
 const priceBandInterval = {'-4':0, '-3':0, '-2':0, '-1':0, '0floor':0,'0ceil':0,'1':0, '2':0, '3':0, '4':0};
    let priceBands = {};
    module.exports = {
        priceBands: function(price){
            let factor = 0;
             if(price>=10000){
                    factor =  100;
                }else if (price>=1000 && price<10000){
                    factor = 50;
                }else if (price>=500 && price<1000){
                    factor = 25;
                }else if (price>=100 && price<500){
                    factor = 10;
                }else if(price>=25 && price<100){
                    factor = 2;
                }else{
                    factor = 0.5;
                }
             let priceCeil, priceFloor;
             if((price%factor) == 0){   
                priceCeil = price + factor;
                priceFloor = price - factor;
             } else {
                const remainder = price%factor;
                priceCeil = price - remainder + factor;
                priceFloor = price - remainder;
             }
            _.map(Object.keys(priceBandInterval), item=>{
                if(parseInt(item)>0){
                    priceBands[item] = (parseInt(item)*factor) + priceCeil;
                } else if (parseInt(item)<0){
                    priceBands[item] = (parseInt(item)*factor) + priceFloor;
                } else {
                    priceBands['0floor'] = priceFloor;
                    priceBands['0ceil'] = priceCeil;
                }
            });
            return priceBands;
        }
    }

如果有人可以分享一些我所缺少的宝贵见解,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

您必须克隆变量priceBandVar,因为javaScript变量是通过引用调用的。以下代码是您的答案:

profitArray = _.map(nfoArray, item => {
  console.log(item.cmp);
  //returns the current market price; getting the correct value here

  let priceBandVar = priceBands(Number(item.cmp));
  console.log(priceBandVar);
  //applying some algorithms; getting the correct value here

  return {
    item: item.cmp,
    profitBand: clone(priceBandVar)
  };
  //Here I find a mismatch between the parameter and the calculations

});

function clone(o) {
  var ret = {};
  Object.keys(o).forEach(function(val) {
    ret[val] = o[val];
  });
  return ret;
}