如何删除嵌套属性?

Posted

技术标签:

【中文标题】如何删除嵌套属性?【英文标题】:How to remove nested attributes? 【发布时间】:2012-10-25 10:02:23 【问题描述】:

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

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

我读过这个问题:Remove a JSON attribute

$(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 对象,搜索这些属性,并在找到它们时删除它们,根据您链接问题中的答案。 警告 12MB .json 问题中的文件 如果您希望在调用 JSON 后执行此操作以获取数据,那将是多余的,因为您必须先下载它们才能删除它们。 @Lübnah - 你能告诉我怎么做吗?因为在链接的问题中没有循环或搜索:/ @JaroslawWaliszko 好的,这有点迂腐,但从技术上讲是正确的。 JSON == javascript 对象表示法。所以,我的意思是 OP 用 JSON 表达的对象。现在开心吗? 【参考方案1】:

你可以这样做:

var postalcodes = YOUR JSON;

for(var code in postalcodes)

 delete postalcodes[code].within_5_km;
 .
 .
 .

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

【讨论】:

【参考方案2】:

转到您提供的 json 网址并打开 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];
         
      
   

【讨论】:

当然,这是在 OP 将 JSON 字符串转换为实际对象之后 我已经尝试过您的解决方案,但它不起作用。我已经用我的代码更新了我的问题。 绝对不需要hasOwnProperty @Lübnah:好吧,我认为他已经很好地解析了。问题没有问如何将 json 字符串转换为 js 对象,而是关于嵌套迭代。 @Railsbeginner:改用$("#json").text(JSON.stringify(data));【参考方案3】:

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

【讨论】:

【参考方案4】:

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

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

【讨论】:

删除一段代码通常不是很有帮助。你能解释一下为什么这应该有效吗?【参考方案5】:

我已经写了一个 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']);

第二个参数支持多路径

【讨论】:

以上是关于如何删除嵌套属性?的主要内容,如果未能解决你的问题,请参考以下文章

如何删除嵌套对象属性[重复]

如何在 ReactJs Redux 中删除/设置状态数组的“null”嵌套属性?

如果祖先的属性使用 xsl 持有某个值,我如何删除属性和元素

如何使用 PHP 删除所有 xml 属性值?

JS:从嵌套数组中删除对象并返回父数组

删除嵌套 CSS flex 上的自动高度分布? [复制]