简体   繁体   English

对象引用给出未定义

[英]Object reference giving undefined

Please bear with me for my silly questions, i am new to javscript. 对于我的愚蠢问题,请耐心等待,我是javscript的新手。 I am trying to filter the map but unable to assign the created object to reference variable. 我正在尝试过滤地图,但无法将创建的对象分配给引用变量。

let serviceFeeMap = new Map();
  paymentAmountServiceFeeInfo.map(serviceFees => {
    if (serviceFees.serviceFees.serviceFeeApplicableMethods.length > 0) {
      serviceFees.serviceFees.serviceFeeApplicableMethods.map(applicableMethod => {
        let serviceFeeList = serviceFeeMap.get(applicableMethod);
        if (!serviceFeeList) {
          serviceFeeMap.set(applicableMethod, new Array(serviceFees.serviceFees));
        } else {
          serviceFeeList.push(serviceFees.serviceFees);
          serviceFeeMap.set(applicableMethod, serviceFeeList);
        }
      });
    }
  });



const finalResult = serviceFeeMap.forEach((v, k) => {
    let values = v;
    let count = 0;
    const arrayValues = values.map((value) => {
      if (count === 0) {
        count++;
         return {
           applicableMethod : k,
           serviceFeeAdjustmentType : value.serviceFeeAdjustmentType,
           serviceFeeAmount : value.serviceFeeAmount
         };
      } else {
        count++;
        return {
          applicableMethod : '',
          serviceFeeAdjustmentType : value.serviceFeeAdjustmentType,
          serviceFeeAmount : value.serviceFeeAmount
        };
      }

    })
    return arrayValues;
  });

I am getting finalResult as undefined . 我得到finalResult作为undefined。

You are trying to return a value from forEach() callback but it can't returns any value. 您正在尝试从forEach()回调返回一个值,但它不能返回任何值。 The forEach() method executes a provided function once per each key/value pair in the Map object, in insertion order. forEach()方法按插入顺序对Map对象中的每个键/值对执行一次提供的函数。

According to MDN documentation 根据MDN 文档

The forEach method executes the provided callback once for each key of the map which actually exist. forEach方法对地图中实际存在的每个键执行一次提供的回调。 It is not invoked for keys which have been deleted. 对于已删除的密钥,不会调用它。 However, it is executed for values which are present but have the value undefined. 但是,它针对存在但未定义的值执行。

That's the reason you are getting undefined of finalResult . 这就是你所得到的原因undefinedfinalResult In this case you can write a global var to hold the iteration value of Map.prototype.forEach() 在这种情况下,你可以写一个全球性的var持有的迭代值Map.prototype.forEach()

let finalResult = [];
serviceFeeMap.forEach((v, k) => {
    let values = v;
    let count = 0;
    const arrayValues = values.map((value) => {
        if (count === 0) {
            count++;
            return {
                applicableMethod : k,
                serviceFeeAdjustmentType : value.serviceFeeAdjustmentType,
                serviceFeeAmount : value.serviceFeeAmount
            };
        } else {
            count++;
            return {
                applicableMethod : '',
                serviceFeeAdjustmentType : value.serviceFeeAdjustmentType,
                serviceFeeAmount : value.serviceFeeAmount
            };
        }

    })
    finalResult.push(arrayValues);
})

That is because forEach will return an undefined . 这是因为forEach将返回undefined You can use a map inside a map which will return array of arrys or create an array outside forEach and push arrayValues inside that 您可以在map内部使用map ,该map将返回arrys数组,也可以在forEach外部创建一个数组,并在其中将arrayValues推送

 const finalResult = serviceFeeMap.map((v, k) => { let values = v; let count = 0; return values.map((value) => { if (count === 0) { count++; return { applicableMethod: k, serviceFeeAdjustmentType: value.serviceFeeAdjustmentType, serviceFeeAmount: value.serviceFeeAmount }; } else { count++; return { applicableMethod: '', serviceFeeAdjustmentType: value.serviceFeeAdjustmentType, serviceFeeAmount: value.serviceFeeAmount }; } }) }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM