0

I have a collection of objects in an array like this:

[
  {
    "NAME": "John Doe",
    "AGE": 25,
    "CITY": "New York",
    "COUNTRY": "USA",
    "GROUP_ID": 1,
    "ACTIVE": 1
  },
  {
    "NAME": "Peter Parker",
    "AGE": 44,
    "CITY": "Los Angeles",
    "COUNTRY": "USA",
    "GROUP_ID": 2,
    "ACTIVE": 1
  },...
]

In my view I only want to display Name, Age, City and Country. Well, my question is how can I remove GROUP_ID and ACTIVE in every object of my collection? I was looking for a solution and found .slice() but I don't know exactly whether is it the right one and how to use this javascript function for each object in an array.

EDIT: For more clarification. My view is defined like below:

<md-list-item ng-repeat="cItems in ::contentItems track by $index">
   <span ng-repeat="(key, value) in cItems track by $index" flex="auto">
      {{ ::value }}
   </span>
   <md-divider></md-divider>
</md-list-item>
10
  • When you are iterating the objects in that array, simply display the properties you are interested in. Why do you want to remove it from the object all together? Commented Nov 17, 2016 at 15:29
  • @yBrodsky Could you give me a better solution to return the values without these to key-value pairs Commented Nov 17, 2016 at 15:30
  • stackoverflow.com/questions/22918613/… Commented Nov 17, 2016 at 15:31
  • 1
    @yBrodsky there's no reason to throw a library at this simple of a problem. Commented Nov 17, 2016 at 15:33
  • 1
    i think it is more an angular question, how to display some properties and some not, and not how to delete the properties from the object. Commented Nov 17, 2016 at 15:33

5 Answers 5

3

You can use the following lines:

contentItems.forEach(function (entry) {
  delete entry['GROUP_ID'];
  delete entry['ACTIVE'];
});
Sign up to request clarification or add additional context in comments.

12 Comments

@yuro By the way this has nothing to do with AngularJS. This is pure JavaScript code.
@yuro If you want to use AngularJS to hide those properties, just don't bind them to any HTML element.
@yuro prepare the data (remove the attributes) before passing it to the ngRepeat.
Ok wait. I will try your solution.
Thanks.. I guess this solution is good enough. Is it right, that the properties don't really removed from the array?
|
0

As mentioned in comments there is no need to delete the keys. You can simple avoid displaying them.

Still if deleting is objective then use delete method

a.forEach(function(item){
delete(item['GROUP_ID']);
delete(item['ACTIVE'])
});

DEMO

Comments

0

Assuming your array is a variable named array:

for ( var i=0,l=array.length; i<l; i++ ) {
  delete array[i]['GROUP_ID'];
  delete array[i]['ACTIVE'];
}

if you're using ES6 you could also do:

for ( let item of array ) {
  delete item['GROUP_ID'];
  delete item['ACTIVE'];
}

Comments

0

You can simply remove properties of an object by using delete. I've added an array of properties to delete but you could delete them directly.

var data = [
  {
    "NAME": "John Doe",
    "AGE": 25,
    "CITY": "New York",
    "COUNTRY": "USA",
    "GROUP_ID": 1,
    "ACTIVE": 1
  },
  {
    "NAME": "Peter Parker",
    "AGE": 44,
    "CITY": "Los Angeles",
    "COUNTRY": "USA",
    "GROUP_ID": 2,
    "ACTIVE": 1
  }
];

var propertiesRemove = ['GROUP_ID', 'ACTIVE']

data.forEach(function(item){
  propertiesRemove.forEach(function(prop){
    delete item[prop];
  });
});

console.log(data);

If you don't want to change your data and it's just a display issue you could render only the properties you want.

<md-list-item ng-repeat="cItems in ::contentItems track by $index">
   <span flex="auto">{{cItems.NAME}}</span>
   <span flex="auto">{{cItems.AGE}}</span>
   <span flex="auto">{{cItems.CITY}}</span>
   <span flex="auto">{{cItems.COUNTRY}}</span>
   <md-divider></md-divider>
</md-list-item>

6 Comments

I know the simple solution of setting the properties in the template. But I need a dynamic solution :).. I guess to use nested forEach()-function is not so good for the performance.
@yuro you can update your ng-repeat with (key, value) in getUpdatedItem(cItems) track by $index" where the function getUpdatedItem returns the item with the properties you want based on the initial element without changing anything. Is that something more aligned with what you want?
Ok... and how looks like the function or how works it?
Add it on your controller and it could be as simple as getUpdatedItem(item) { return { NAME: item. NAME, AGE: item.AGE, CITY: item.CITY, COUNTRY: item.COUNTRY } }. Can update answer with it if you want
it is also good. But the problem is when I use this dynamically. I always need to define all necessary properties and that's tedious.
|
0

Actually to display required information in angular we don't need to remove other elements from array in template we can go with limited information.

ANGULAR CODE

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records = [
  {
    "NAME": "John Doe",
    "AGE": 25,
    "CITY": "New York",
    "COUNTRY": "USA",
    "GROUP_ID": 1,
    "ACTIVE": 1
  },
  {
    "NAME": "Peter Parker",
    "AGE": 44,
    "CITY": "Los Angeles",
    "COUNTRY": "USA",
    "GROUP_ID": 2,
    "ACTIVE": 1
  }
]
});

Angular Template

<body ng-app="myApp" ng-controller="myCtrl">
<ul ng-repeat="record in records">
<li>{{record.NAME}}</li>
<li>{{record.AGE}}</li>
<li>{{record.COUNTRY}}</li>
</ul>

But as you are asking following procedure will answer your question.

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $data = [
  {
    "NAME": "John Doe",
    "AGE": 25,
    "CITY": "New York",
    "COUNTRY": "USA",
    "GROUP_ID": 1,
    "ACTIVE": 1
  },
  {
    "NAME": "Peter Parker",
    "AGE": 44,
    "CITY": "Los Angeles",
    "COUNTRY": "USA",
    "GROUP_ID": 2,
    "ACTIVE": 1
  }
]; 
$scope.records = $data.map(function(item){
delete(item['GROUP_ID']);

delete(item['ACTIVE']);
return item; 
});
});

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.