2017-01-23 44 views
0

我想循环访问JSON响应,检查key和value是否存在,如果不存在,则将其添加到数组中。检查obj是否存在并推送到数组

$scope.InitProdToArray = function (data) { 
    angular.forEach($scope.data.obj.Product, function(value, index) { 

     if (value.Product_type != 'T') { 
      $scope.data.obj.Product.push({Product_type: 'T'}); 
     } 
     if (value.Product_type != '16364NB') { 
      $scope.data.obj.Product.push({Product_type: '16364NB'}); 
     } 
     if (value.Product_type != '39087NB') { 
      $scope.data.obj.Product.push({Product_type: '39087NB'}); 
     } 
     if (value.Product_type != 'C') { 
      $scope.data.obj.Product.push({Product_type: 'C'}); 
     } 
     if (value.Product_type != '4NB') { 
      $scope.data.obj.Product.push({Product_type: '4NB'}); 
     }   
    }); 

    JSON: $scope.data.obj.Product = 
        [{ 
         "Count": 28, 
         "Product_type": "T" 
        }, { 
         "Count": 88, 
         "Product_type": "4NB" 
        }, { 
         "Count": 20, 
         "Product_type": "C" 
        }, { 
         "Count": 3, 
         "Product_type": "39087NB" 
        } 
       ] 

这似乎不起作用,因为每次迭代通过每个节点时,我都会推送密钥值对。我最终得到了一个具有相同的多个product_type的JSON。

有没有办法阻止代码添加额外的键值,如果它已经存在?

+0

是的,你可以先检查对象的数组是否存在的关键。 – choz

+0

http://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object如何检查 –

回答

1

你似乎已经写上你的类型检查颠倒了。 而不是if (value.Product_type != 'T') {...我会想象像if (value.Product_type == 'T') {...这种方式,只有当类型匹配时才会推到Product数组。

除此之外,您还可以检查推,如果你Product阵列已经包含了类型的键之前:if($scope.data.obj.Product.indexOf(value.Product_type)!==undefined)

0

从你的代码好像你正在做的搜索,并插入,而不是事情要做关键/值。

如果您仅支持新浏览器,则为此可以使用称为find的便利功能。

var productTypes = ['T', '16364NB', '39087NB', 'C', '4NB']; 

angular.forEach(productTypes, function(value) { 
    var exists = $scope.data.obj.Product.find(function(item) { 
     return (item.Product_type == value); 
    }); 

    if (!exists) { 
     $scope.data.obj.Product.push({ 
      Product_type: value, 
      count: 0 
     }); 
    } 
}); 

如果您需要支持旧的浏览器,你将需要添加polyfillfind为好。

0

var TYPES = { T: 'T', NB4: '4NB', C: 'C', NB39087: '39087NB' \t }; 
 
var products = [{ "Count": 28, "Product_type": "T" }, { "Count": 88, "Product_type": "4NB" }]; 
 

 
/* check if the element key is not the specified value....*/ 
 
function isKeyInElement(keyToCheck, element) { 
 
\t return element.Product_type === keyToCheck; 
 
} 
 

 
/* checks if there are NO elements in array with the specified key ...*/ 
 
function isKeyInArray(keyToCheck, array) { 
 
\t return !!array.filter(function (element) { return isKeyInElement(keyToCheck, element); }).length; 
 
} 
 

 
/* another way to check if there are NO elements in array with the specified key ...*/ 
 
function isKeyInArray2(keyToCheck, array) { 
 
\t return array.map(function (element) { return element.Product_type; }).indexOf(keyToCheck) > -1; 
 
} 
 

 
function initProdToArray(source) { 
 
\t if (!isKeyInArray(TYPES.T, source)) { source.push({ Product_type: TYPES.T }); } 
 
\t if (!isKeyInArray(TYPES.NB4, source)) { source.push({ Product_type: TYPES.NB4 }); } 
 
\t if (!isKeyInArray(TYPES.C, source)) { source.push({ Product_type: TYPES.C }); } 
 
\t if (!isKeyInArray(TYPES.NB39087, source)) { source.push({ Product_type: TYPES.NB39087 }); } 
 
} 
 

 
initProdToArray(products); 
 
console.log(products);