简体   繁体   中英

Object reference giving undefined

Please bear with me for my silly questions, i am new to 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 .

You are trying to return a value from forEach() callback but it can't returns any value. The forEach() method executes a provided function once per each key/value pair in the Map object, in insertion order.

According to MDN documentation

The forEach method executes the provided callback once for each key of the map which actually exist. 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 . In this case you can write a global var to hold the iteration value of 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 . 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

 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 }; } }) }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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