无法正确初始化Javascript对象

时间:2017-12-21 11:42:42

标签: javascript

我正在尝试使用另一个数组作为输入创建一个对象数组。我有以下循环代码:

    // Setup product key for local items
    for (var i=0; i<$scope.listDetails.details.length ; i++) {
        if ($scope.listDetails.details[i].productIdentifier2 == 'Local') {
            var productFound = false;
            for (var j=0; j< $scope.allproducts.length ; j++) {
                if ($scope.listDetails.details[i].category == $scope.allproducts[j].category && $scope.listDetails.details[i].productname == $scope.allproducts[j].productname && $scope.listDetails.details[i].productIdentifier2 == $scope.allproducts[j].productIdentifier2) {
                    $scope.listDetails.details[i].productKey = $scope.allproducts[j].productKey;
                    productFound = true;
                    break;
                }
            }
            // Group all products to be inserted
            if (productFound==false) {
                var productDetailSingle = {};
                console.log("productDetail init");
                console.log(productDetailSingle);
                productDetailSingle.category = $scope.listDetails.details[i].cateogry;
                productDetailSingle.itemStatus = $scope.listDetails.details[i].itemStatus;
                productDetailSingle.productIdentifier1 = $scope.listDetails.details[i].productcode;
                productDetailSingle.productIdentifier2 = $scope.listDetails.details[i].productIdentifier2;
                productDetailSingle.productName = $scope.listDetails.details[i].productname;
                productDetailSingle.productname = $scope.listDetails.details[i].productname;
                productDetailSingle.productcode = $scope.listDetails.details[i].productcode;
                console.log("productDetail push");
                console.log(productDetailSingle);
                productsToAdd.push(productDetailSingle);
            }
        }
    }

我遇到的问题是,当我记录productDetailSingle对象时,我得到的其他属性我没有初始化。以下是条件if(productFound == false):

的控制台日志的输出
[Log] productDetail init (console-via-logger.js, line 173)
[Log] {} (console-via-logger.js, line 173)
[Log] productDetail push (console-via-logger.js, line 173)
[Log] Object (console-via-logger.js, line 173)

category: undefined
itemStatus: "OPEN"
productIdentifier1: 0
productIdentifier2: "Local"
productName: "mushroom "
productcode: 0
productname: "mushroom "

Object Prototype

[Log] productDetail init (console-via-logger.js, line 173)
[Log] {} (console-via-logger.js, line 173)
[Log] productDetail push (console-via-logger.js, line 173)
[Log] Object (console-via-logger.js, line 173)

available: "Y"
category: undefined
itemStatus: "OPEN"
price: null
productDesc: null
productIdentifier1: 0
productIdentifier2: "Local"
productKey: 53
productName: "nido milk"
productcode: 0
productname: "nido milk"
uuid: "BAAFE64E-CF76-4156-A09F-80F678E3A2B0"

对象原型

如您所见,第一个对象已正确初始化。但是,对于第二个对象,虽然我最初将其设置为{},但是还有其他属性已添加到第二个对象。有没有人遇到过这种类型的问题?

1 个答案:

答案 0 :(得分:0)

使用函数编程...

const productsToAdd = $scope.listDetails.details
  .filter(detail => detail.productIdentifier2 === 'local')
  .map(detail => {
    const productKey = $scope.allproducts
      .filter(product => (detail.category && detail.productName === product.productName && detail.productIdentifier2 === product.productIdentifier2))
      .map(product => product.productKey)
      .reduce((memo, productKey) => productKey, false);

    if(!productKey) {
      return {
        category: detail.category,
        itemStatus: detail.itemStatus,
        productIdentifier1: detail.productIdentifier1,
        productIdentifier2: detail.productIdentifier2,
        productName: detail.productName,
        productcode: detail.productcode
      };
    } else {
      detail.productKey = productKey 
    }

    return false;
  })
   //remove the falses from above
  .filter(productToAdd => productToAdd);