2016-08-24 3 views
1

この変数に$ scope.bbTreeDataというJSONオブジェクトがあります。フラグがfalseのオブジェクトを削除しようとしています。入れ子になったJSONオブジェクトをトラバースすることはできますが、オブジェクトを削除する方法がわかりません。なにか提案を ?インデックスを追加し、あなたがルートを削除したい場合はHow do I remove a property from a JavaScript object?Nested JSON、angularjsを使用してJqueryの条件に基づいてオブジェクトを削除します。

:私はのための答えがすでに存在していると信じて

delete item.subItem[index] 

:あなたは、プレーンJavaScriptでdeleteキーワードを使用することができます

[{ 
    "market": "Atl", 
    "subItem": [{ 
    "comment_id": "1", 
    "user_id": "32509", 
    "flag": true 
    }, { 
    "comment_id": "2", 
    "user_id": "32510", 
    "flag": false 

    }] 
}, { 
    "market": "Chicago", 
    "subItem": [{ 
    "comment_id": "3", 
    "user_id": "32501", 
    "flag": true 
    }, { 
    "comment_id": "4", 
    "user_id": "32502", 
    "flag": false 

    }] 
}] 

$scope.bbTreeInactiveData = angular.copy($scope.bbTreeData); 
angular.forEach($scope.bbTreeInactiveData, function(item) { 
    angular.forEach(item.subItem, function(record, index) { 
    if (record.flag == false) { 
     console.log(item.subItem, index); 
     /* code to remove the object*/ 
    } 
    }); 
}); 
+0

Array.forEachの代わりにArray.filterを使用します。または、Array.splice – Shilly

答えて

2

がすべてで配列のコピーを返しますdocumentation


_.without(配列、値)なしで見ます削除された値のインスタンス

_.outout([1,2,1,0,3,1,4]、0,1);
=> [2、3、4]

入力

[ 
    { 
     "market": "Atl", 
     "subItem": [ 
      { 
       "comment_id": "1", 
       "user_id": "32509", 
       "flag": true 
      }, 
      { 
       "comment_id": "2", 
       "user_id": "32510", 
       "flag": false 
      } 
     ] 
    }, 
    { 
     "market": "Chicago", 
     "subItem": [ 
      { 
       "comment_id": "3", 
       "user_id": "32501", 
       "flag": true 
      }, 
      { 
       "comment_id": "4", 
       "user_id": "32502", 
       "flag": false 
      } 
     ] 
    } 
] 

出力

[ 
    { 
     "market": "Atl", 
     "subItem": [ 
      { 
       "comment_id": "1", 
       "user_id": "32509", 
       "flag": true 
      } 
     ] 
    }, 
    { 
     "market": "Chicago", 
     "subItem": [ 
      { 
       "comment_id": "3", 
       "user_id": "32501", 
       "flag": true 
      } 
     ] 
    } 
] 

コードスニペット

var json = JSON.parse('[{"market":"Atl","subItem":[{"comment_id":"1","user_id":"32509","flag":true},{"comment_id":"2","user_id":"32510","flag":false}]},{"market":"Chicago","subItem":[{"comment_id":"3","user_id":"32501","flag":true},{"comment_id":"4","user_id":"32502","flag":false}]}]'); 
 

 
for(var i=0; i<json.length; i++) { 
 
    json[i].subItem = _.without(json[i].subItem, _.findWhere(json[i].subItem, {flag: false})); 
 
}; 
 

 
console.log(JSON.stringify(json, 0, 8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

+0

var json = JSON .parse( '[{"market": "Atl"、 "subItem":[{"comment_id": "1"、 "user_id": "32509"、 "flag":false}、{"comment_id": "2 "32510"、 "flag":false}]}、{"market": "Chicago"、 "subItem":[{"comment_id": "3"、 "user_id": "32501"、 "user_id" "flag":true}、{"comment_id": "4"、 "user_id": "32502"、 "flag":false}]}] '); – user1005310

+0

JSONを変更した場合、希望の結果が得られません。私はすべての副項目を変更しました。偽にフラグを付けます – user1005310

+0

@ user1005310この行はテストデータを含めるだけです。この行を省略してそのままObjectとして続けることができます。 –

0

パラメータを最初のforEachに設定し、配列スプライス機能付きルートを削除します。

$scope.bbTreeInactiveData.splice(indexRoot,1); 
あなたは _underscorejs

_without()機能を使用することができます3210

+0

を使用して手動で配列からレコードをスプライスします。これによりサブアイテムが削除されますが、すべてのサブアイテムが削除されるシナリオでは、ルート要素は残っています。ルートレベルの要素を削除するにはどうすればよいですか?たとえば、すべてのサブアイテムフラグがfalseとして設定されているマーケットアクトを削除します。 – user1005310

+0

「アイテムを削除する[indexRoot]」と同じように簡単です。最初の 'forEach'のインデックスを追加する必要があります。 –

+0

実際には、ルート要素は配列なので、2番目の答えは、探している要素ではなく、削除された要素ごとに 'undefined'を返します。 –

1

これを試してみてください:

$scope.bbTreeInactiveData = angular.copy($scope.bbTreeData); 

var results = $scope.bbTreeInactiveData.map(function(row) { 
    return row.subItem.filter(function(cell) { 
    return cell.flag == true 
    }); 
}); 

使用map()filter()関数を。

+0

さらに処理するために元のデータが必要な場合は、コピーを作成しても安全です。 –

関連する問題