0

Hi I have a Angular data as follows,

{"category":"B","check_histories":[{"date_created":"2015-10-07T10:35:38","field_name":"category","new_value":"B","old_value":null,"resource_uri":"","type":1,"username":"e1007476"},{"date_created":"2015-10-07T10:35:39","field_name":"check_name","new_value":"222","old_value":null,"resource_uri":"","type":1,"username":"e1007476"},{"date_created":"2015-10-07T10:37:25","field_name":"check_name","new_value":"22233","old_value":"222","resource_uri":"","type":0,"username":"e1007476"}],"check_name":"22233","date_created":"2015-10-07T10:35:38","date_modified":"2015-10-07T10:37:25","id":134,"is_valid":true,"resource_uri":""}

How to remove all the data EXCEPT check_histories from the data, when I click a remove button. I have tried

$scope.clear = function(index) {
        $scope.final_data.checks.splice(index, 1);
    }; 

But it deleted the entire data, instead of the desired part. Am quite new to AngualrJS. Please help me on it. Thanks in advance.

2
  • you only want to keep check_histories property ? Commented Oct 7, 2015 at 3:11
  • Yes, thats right Anik Commented Oct 7, 2015 at 3:14

2 Answers 2

1

You can use Javascript "delete" as following:

delete $scope.final_data.checks.category;
// or,
delete $scope.final_data.checks['category'];
// or,
var prop = "category";
delete $scope.final_data.checks[prop];

In your case you have to loop through your object property and delete them:

angular.forEach($scope.final_data, function(value, key) {
   if(key !== 'check_histories') {
      delete $scope.final_data[key];
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The Underscore way:

$scope.final_data.checks = _.pick($scope.final_data.checks, 'check_histories');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.