2017-01-23 2 views
0

JSONレスポンスを反復したい場合は、キーと値が存在するかどうかを確認し、そうでない場合は配列に追加します。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') {...のように、タイプが一致する場合にのみ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 
     }); 
    } 
}); 

、あなたにもfindためpolyfillを追加する必要があります。

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

関連する問題