从Object数组中删除所有键

时间:2015-10-23 06:02:38

标签: javascript arrays json angularjs

我有一个看起来像的数组:

var standardsList = [
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08932"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "07932"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08332"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08132"},
{"Grade": "Math K", "Domain": "Geometry", "$$hashKey": "08902"} ];

$$hashKey属性默认情况下由angularJS添加

我想从上面数组的所有对象中删除此$$hashkey属性值。

任何人指点?

3 个答案:

答案 0 :(得分:2)

重复提问/回答:What is the $$hashKey added to my JSON.stringify result

Angular添加了这个以跟踪您的更改,因此它知道何时需要更新DOM。

如果使用angular.toJson(obj)而不是JSON.stringify(obj),那么Angular将为您删除这些内部使用值。

此外,如果您通过{uniqueProperty}后缀更改重复表达式以使用轨道,Angular根本不必添加$$ hashKey。

否则此代码将删除它:



var standardsList = [
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08932"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "07932"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08332"},
{"Grade": "Math K", "Domain": "Counting & Cardinality", "$$hashKey": "08132"},
{"Grade": "Math K", "Domain": "Geometry", "$$hashKey": "08902"} ];

for (var i=0; i<standardsList.length; i++){
  delete standardsList[i].$$hashKey;
}

console.log(JSON.stringify(standardsList));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用map函数执行此操作:

var newList = standardsList.map(function(obj){
   delete obj['$$hashKey'];

   return obj;
});

答案 2 :(得分:-1)

一种解决方案是使用for each循环:

for each (element in standardsList)
{
    delete element.$$hashKey;
}