关于javascript:如何删除嵌套属性?

How to remove nested attributes?

我有这个json文件:http://danish-regional-data.googlecode.com/svn/trunk/danish_regional_data.json

如何删除所有邮政编码的所有属性within_5_kmwithin_10_kmwithin_25_kmwithin_50_kmwithin_100_km

我已经阅读过这个问题:删除一个json属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$(document).ready(function() {

    $.getJSON("post.json", function(data) {

    var pc = data.postalcodes;
    for (var id in pc) {
       if(pc.hasOwnProperty(id)) {
          for(var attr in pc[id]) {
             if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
               delete pc[id][attr];
             }
          }
       }
    }

    $("#json").html(pc);

    });

});


在ES2016中,可以使用析构函数为子集对象选择所需的字段。

1
2
3
4
5
6
7
8
9
10
11
//ES6 subset of an object by specific fields
var object_private = {name:"alex", age: 25, password: 123};
var {name,age} = object_private, object_public = {name,age}


//method 2 using literals
let object_public = (({name,age})=>({name,age}))(object_private);


//use map if array of objects
    users_array.map(u=>u.id)


转到您提供的JSON URL并打开Firebug控制台。然后放入下面的代码并执行它:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var p = document.getElementsByTagName('pre');
for(i=0; i < p.length; i++) {

  var data = JSON.parse(p[i].innerHTML);
  var pc = data.postalcodes;

  // this is the code i gave you... the previous is jsut to pull it out of the page
  // in Firebug - this works for me

  for (var id in pc) {
     if(pc.hasOwnProperty(id)) {
        for(var attr in pc[id]) {
          if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
             console.log('Deleting postalcodes.'+id+'.'+attr);
             delete pc[id][attr];
           }
        }
     }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
// assume data is the complete json

var pc = data.postalcodes;
for (var id in pc) {
   if(pc.hasOwnProperty(id)) {
      for(var attr in pc[id]) {
         if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
           delete pc[id][attr];
         }
      }
   }
}


我已经编写了一个NPM模块unset,它正是这样做的。您可以指定类似于JSON路径模块的JSON路径,直到要删除的叶属性。

1
2
3
let unset = require('unset');
let object = {a: { b: [ {x: 1}, {x: [{ e: 2} ]}]}};
let newObject = unset(object, ['/a/b[*]/x']);

第二个参数支持多个路径


JSON截断:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var data = {"postalcodes":
{"800":{"id":"800","name":"H\u00f8je Taastrup","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["169"],"commune_names":["H\u00f8je-Taastrup"],"lat":"55.66713","lng":"12.27888","within_5_km":["800","2620","2630","2633"],"within_10_km":["800","2600","2605","2620"]},
"900":{"id":"900","name":"K\u00f8benhavn C","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["101"],"commune_names":["K\u00f8benhavns"],"lat":"55.68258093401054","lng":"12.603657245635986","within_5_km":["900","999"]},
"1417":{"commune_id":"390","region_id":"1085"}}};
var pc = data.postalcodes;
for (var id in pc) {
    var entry = pc[id];
    for(var attr in entry) {
        if(attr.indexOf('within_') === 0) {
            delete entry[attr];
        }
    }
}
console.dir(data); // your data object has been augmented at this point

也可以使用正则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var data = {"postalcodes":
{"800":{"id":"800","name":"H\u00f8je Taastrup","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["169"],"commune_names":["H\u00f8je-Taastrup"],"lat":"55.66713","lng":"12.27888","within_5_km":["800","2620","2630","2633"],"within_10_km":["800","2600","2605","2620"]},
"900":{"id":"900","name":"K\u00f8benhavn C","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["101"],"commune_names":["K\u00f8benhavns"],"lat":"55.68258093401054","lng":"12.603657245635986","within_5_km":["900","999"]},
"1417":{"commune_id":"390","region_id":"1085"}}};
var regexp = new RegExp("^within_","i");   // case insensitive regex matching strings starting with within_
var pc = data.postalcodes;
for (var id in pc) {
    var entry = pc[id];
    for(var attr in entry) {
        if(regexp.test(attr)) {
            delete entry[attr];
        }
    }
}
console.dir(data);

你可以这样做:

1
2
3
4
5
6
7
8
9
var postalcodes = YOUR JSON;

for(var code in postalcodes)
{
 delete postalcodes[code].within_5_km;
 .
 .
 .
}

您可能需要检查代码是否包含您的属性…