如何删除嵌套属性?

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

如何删除所有邮政编码中within_5_kmwithin_10_km ,内within_25_kmwithin_50_km ,内within_100_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); }); }); 

转到您提供的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]; } } } } 

在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) 

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

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

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

第二个参数支持多个路径

你能做到这一点:

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

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