简体   繁体   English

如何在AngularJS中删除或隐藏数组的键值对

[英]How to remove or hide a key-value pair of an array in AngularJS

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? 好吧,我的问题是如何删除集合中每个对象的GROUP_IDACTIVE 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. 我一直在寻找一种解决方案,找到了.slice()但我不知道它是否正确,以及如何对数组中的每个对象使用此javascript函数。

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>

You can use the following lines: 您可以使用以下几行:

contentItems.forEach(function (entry) {
  delete entry['GROUP_ID'];
  delete entry['ACTIVE'];
});

Assuming your array is a variable named array : 假设您的数组是一个名为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: 如果您使用的是ES6,还可以执行以下操作:

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

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 如果删除是客观的,那就使用delete方法

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

DEMO 演示

You can simply remove properties of an object by using delete . 您可以简单地通过使用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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM