5

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

如何删除所有邮政编码的所有属性within_5_km, within_10_km, within_25_km, within_50_km, within_100_km

我读过这个问题:删除 JSON 属性

$(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);

    });

});
4

5 回答 5

3

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

//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)
于 2016-04-29T20:29:14.053 回答
2

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

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

// 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];
         }
      }
   }
}
于 2012-11-05T14:51:31.597 回答
1

JSON 被截断:

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

你也可以使用正则表达式

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);
于 2012-11-05T15:32:20.520 回答
1

我写了一个 npm 模块unset正是这样做的。您指定类似于json-path模块的 json 路径,直到要删除的叶属性。

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

第二个参数支持多个路径

于 2017-01-05T10:58:23.607 回答
0

那么你可以这样做:

var postalcodes = YOUR JSON;

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

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

于 2012-11-05T14:44:01.580 回答