javascript - 如何在递归解析后正确显示 JSON 对象的结果?

标签 javascript json

我有一些代码可以根据给定的键数组递归地删除 JSON 对象中的属性。它能够找到删除所有相关键,但我不知道如何显示最终结果将所有键都删除。

我在 for 循环之后添加了以下内容:

if (index == -1) {
    console.log(obj)
}

想法是,一旦它到达顶部并完​​成 for 循环,它应该显示新对象,问题在于它在显示时缺少很多属性的根键。

function deleteJSONProperties (obj, keys){
    var index;
    for (var prop in obj) {
        if(obj.hasOwnProperty(prop)){
            switch(typeof(obj[prop])){
                case 'string':
                    index = keys.indexOf(prop);
                    if(index > -1){
                        delete obj[prop];
                    }
                break;
                case 'object':
                    index = keys.indexOf(prop);
                    if(index > -1){
                        delete obj[prop];
                    }else{
                        deleteJSONProperties (obj[prop], keys);
                    }
                break;
                case 'boolean':
                    index = keys.indexOf(prop);
                    if(index > -1){
                        delete obj[prop];
                    }
                break;
                case 'number':
                    index = keys.indexOf(prop);
                    if(index > -1){
                        delete obj[prop];
                    }
                break;
            }
        }
    }
}

这是一些测试数据:

var jsonObj2 = [
    {
      "_id": "5d14e86629cba445323ab05a",
      "age": 20,
      "tags": [
        "minim",
        "occaecat",
        "veniam",
        "consectetur"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Alfreda Boone"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Mcgee Oneill"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Kaye Dejesus"
        }
      ],
      "greeting": "Hello, undefined! You have 6 unread messages.",
      "favoriteFruit": "banana"
    },
    {
      "_id": "5d14e866b05cc8b27c2361d9",
      "age": 23,
      "tags": [
        "consequat",
        "officia",
        "consectetur",
        "fugiat"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Brooke Smith"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Hodges Nielsen"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Lesa Hall"
        }
      ],
      "greeting": "Hello, undefined! You have 3 unread messages.",
      "favoriteFruit": "apple"
    },
    {
      "_id": "5d14e866db71274862f509be",
      "age": 32,
      "tags": [
        "aute",
        "officia",
        "esse",
        "voluptate"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Rivers Anderson"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Ingram Mccall"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Melton Quinn"
        }
      ],
      "greeting": "Hello, undefined! You have 9 unread messages.",
      "favoriteFruit": "banana"
    },
    {
      "_id": "5d14e866ceeadef742940bf0",
      "age": 27,
      "tags": [
        "mollit",
        "laboris",
        "consequat",
        "nisi"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Delia Woodward"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Kristin Riley"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Floyd Lowe"
        }
      ],
      "greeting": "Hello, undefined! You have 4 unread messages.",
      "favoriteFruit": "apple"
    },
    {
      "_id": "5d14e866de69ec724cea8da4",
      "age": 30,
      "tags": [
        "velit",
        "fugiat",
        "aute",
        "deserunt"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Osborn Hubbard"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Dianna Daugherty"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Sims Guy"
        }
      ],
      "greeting": "Hello, undefined! You have 10 unread messages.",
      "favoriteFruit": "apple"
    },
    {
      "_id": "5d14e86643a18516e353146a",
      "age": 34,
      "tags": [
        "id",
        "veniam",
        "voluptate",
        "esse"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Solis Nolan"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Maricela Colon"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Gilda Ortiz"
        }
      ],
      "greeting": "Hello, undefined! You have 6 unread messages.",
      "favoriteFruit": "apple"
    },
    {
      "_id": "5d14e8662452730e42d257d4",
      "age": 23,
      "tags": [
        "velit",
        "cupidatat",
        "duis",
        "dolore"
      ],
      "friends": [
        {
          "inheritedValue": 0,
          "ExpectedValue": "Cole Roman"
        },
        {
          "inheritedValue": 1,
          "ExpectedValue": "Kimberley Carney"
        },
        {
          "inheritedValue": 2,
          "ExpectedValue": "Lowery Mcdonald"
        }
      ],
      "greeting": "Hello, undefined! You have 9 unread messages.",
      "favoriteFruit": "strawberry"
    }
  ];

var keys = ['inheritedValue', 'effectiveValue','ExpectedValue'];
deleteJSONProperties(jsonObj2, keys);

预期的输出应该是删除了键的原始 JSON 对象。使用我的解决方案,我丢失了一些信息,例如而不是显示

"tags": [
        "minim",
        "occaecat",
        "veniam",
        "consectetur"
      ]

我会:

["minim",
 "occaecat",
 "veniam",
 "consectetur"
]

有条件的实际结果

[ 'minim', 'occaecat', 'veniam', 'consectetur' ]
[ {}, {}, {} ]
{ _id: '5d14e86629cba445323ab05a',
  age: 20,
  tags: [ 'minim', 'occaecat', 'veniam', 'consectetur' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 6 unread messages.',
  favoriteFruit: 'banana' }
[ 'consequat', 'officia', 'consectetur', 'fugiat' ]
[ {}, {}, {} ]
{ _id: '5d14e866b05cc8b27c2361d9',
  age: 23,
  tags: [ 'consequat', 'officia', 'consectetur', 'fugiat' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 3 unread messages.',
  favoriteFruit: 'apple' }
[ 'aute', 'officia', 'esse', 'voluptate' ]
[ {}, {}, {} ]
{ _id: '5d14e866db71274862f509be',
  age: 32,
  tags: [ 'aute', 'officia', 'esse', 'voluptate' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 9 unread messages.',
  favoriteFruit: 'banana' }
[ 'mollit', 'laboris', 'consequat', 'nisi' ]
[ {}, {}, {} ]
{ _id: '5d14e866ceeadef742940bf0',
  age: 27,
  tags: [ 'mollit', 'laboris', 'consequat', 'nisi' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 4 unread messages.',
  favoriteFruit: 'apple' }
[ 'velit', 'fugiat', 'aute', 'deserunt' ]
[ {}, {}, {} ]
{ _id: '5d14e866de69ec724cea8da4',
  age: 30,
  tags: [ 'velit', 'fugiat', 'aute', 'deserunt' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 10 unread messages.',
  favoriteFruit: 'apple' }
[ 'id', 'veniam', 'voluptate', 'esse' ]
[ {}, {}, {} ]
{ _id: '5d14e86643a18516e353146a',
  age: 34,
  tags: [ 'id', 'veniam', 'voluptate', 'esse' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 6 unread messages.',
  favoriteFruit: 'apple' }
[ 'velit', 'cupidatat', 'duis', 'dolore' ]
[ {}, {}, {} ]
{ _id: '5d14e8662452730e42d257d4',
  age: 23,
  tags: [ 'velit', 'cupidatat', 'duis', 'dolore' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 9 unread messages.',
  favoriteFruit: 'strawberry' }
[ { _id: '5d14e86629cba445323ab05a',
    age: 20,
    tags: [ 'minim', 'occaecat', 'veniam', 'consectetur' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 6 unread messages.',
    favoriteFruit: 'banana' },
  { _id: '5d14e866b05cc8b27c2361d9',
    age: 23,
    tags: [ 'consequat', 'officia', 'consectetur', 'fugiat' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 3 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e866db71274862f509be',
    age: 32,
    tags: [ 'aute', 'officia', 'esse', 'voluptate' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 9 unread messages.',
    favoriteFruit: 'banana' },
  { _id: '5d14e866ceeadef742940bf0',
    age: 27,
    tags: [ 'mollit', 'laboris', 'consequat', 'nisi' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 4 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e866de69ec724cea8da4',
    age: 30,
    tags: [ 'velit', 'fugiat', 'aute', 'deserunt' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 10 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e86643a18516e353146a',
    age: 34,
    tags: [ 'id', 'veniam', 'voluptate', 'esse' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 6 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e8662452730e42d257d4',
    age: 23,
    tags: [ 'velit', 'cupidatat', 'duis', 'dolore' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 9 unread messages.',
    favoriteFruit: 'strawberry' } ]

for循环结束时没有条件的实际结果

[ 'minim', 'occaecat', 'veniam', 'consectetur' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e86629cba445323ab05a',
  age: 20,
  tags: [ 'minim', 'occaecat', 'veniam', 'consectetur' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 6 unread messages.',
  favoriteFruit: 'banana' }
[ 'consequat', 'officia', 'consectetur', 'fugiat' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e866b05cc8b27c2361d9',
  age: 23,
  tags: [ 'consequat', 'officia', 'consectetur', 'fugiat' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 3 unread messages.',
  favoriteFruit: 'apple' }
[ 'aute', 'officia', 'esse', 'voluptate' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e866db71274862f509be',
  age: 32,
  tags: [ 'aute', 'officia', 'esse', 'voluptate' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 9 unread messages.',
  favoriteFruit: 'banana' }
[ 'mollit', 'laboris', 'consequat', 'nisi' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e866ceeadef742940bf0',
  age: 27,
  tags: [ 'mollit', 'laboris', 'consequat', 'nisi' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 4 unread messages.',
  favoriteFruit: 'apple' }
[ 'velit', 'fugiat', 'aute', 'deserunt' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e866de69ec724cea8da4',
  age: 30,
  tags: [ 'velit', 'fugiat', 'aute', 'deserunt' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 10 unread messages.',
  favoriteFruit: 'apple' }
[ 'id', 'veniam', 'voluptate', 'esse' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e86643a18516e353146a',
  age: 34,
  tags: [ 'id', 'veniam', 'voluptate', 'esse' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 6 unread messages.',
  favoriteFruit: 'apple' }
[ 'velit', 'cupidatat', 'duis', 'dolore' ]
{}
{}
{}
[ {}, {}, {} ]
{ _id: '5d14e8662452730e42d257d4',
  age: 23,
  tags: [ 'velit', 'cupidatat', 'duis', 'dolore' ],
  friends: [ {}, {}, {} ],
  greeting: 'Hello, undefined! You have 9 unread messages.',
  favoriteFruit: 'strawberry' }
[ { _id: '5d14e86629cba445323ab05a',
    age: 20,
    tags: [ 'minim', 'occaecat', 'veniam', 'consectetur' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 6 unread messages.',
    favoriteFruit: 'banana' },
  { _id: '5d14e866b05cc8b27c2361d9',
    age: 23,
    tags: [ 'consequat', 'officia', 'consectetur', 'fugiat' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 3 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e866db71274862f509be',
    age: 32,
    tags: [ 'aute', 'officia', 'esse', 'voluptate' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 9 unread messages.',
    favoriteFruit: 'banana' },
  { _id: '5d14e866ceeadef742940bf0',
    age: 27,
    tags: [ 'mollit', 'laboris', 'consequat', 'nisi' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 4 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e866de69ec724cea8da4',
    age: 30,
    tags: [ 'velit', 'fugiat', 'aute', 'deserunt' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 10 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e86643a18516e353146a',
    age: 34,
    tags: [ 'id', 'veniam', 'voluptate', 'esse' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 6 unread messages.',
    favoriteFruit: 'apple' },
  { _id: '5d14e8662452730e42d257d4',
    age: 23,
    tags: [ 'velit', 'cupidatat', 'duis', 'dolore' ],
    friends: [ {}, {}, {} ],
    greeting: 'Hello, undefined! You have 9 unread messages.',
    favoriteFruit: 'strawberry' } ]

最佳答案

你没有删除空对象,所以它们仍然是空的,现在好吗?

'use strict';
function deleteJSONProperties (obj, keys){
    var index;
    var itemsNo = 0;
    for (var prop in obj) {
        itemsNo++;
        if(obj.hasOwnProperty(prop)){
            switch(typeof(obj[prop])){
                case 'string':
                    index = keys.indexOf(prop);
                    if(index > -1){
                      itemsNo--;
                      delete obj[prop];
                    }
                break;
                case 'object':
                    index = keys.indexOf(prop);
                    if(index > -1){
                      itemsNo--;
                      delete obj[prop];
                    }else{
                        if(!deleteJSONProperties (obj[prop], keys)) {
                          itemsNo--;
                          delete obj[prop];
                        }
                    }
                break;
                case 'boolean':
                    index = keys.indexOf(prop);
                    if(index > -1){
                      itemsNo--;
                      delete obj[prop];
                    }
                break;
                case 'number':
                    index = keys.indexOf(prop);
                    if(index > -1){
                      itemsNo--;
                      delete obj[prop];
                    }
                break;
            }
        } else itemsNo--;
    }
    if(!itemsNo) delete obj[prop];
    return itemsNo;
}

var jsonObj2 = [
  {
    "_id": "5d14e86629cba445323ab05a",
    "age": 20,
    "tags": [
      "minim",
      "occaecat",
      "veniam",
      "consectetur"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Alfreda Boone"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Mcgee Oneill"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Kaye Dejesus"
      }
    ],
    "greeting": "Hello, undefined! You have 6 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "5d14e866b05cc8b27c2361d9",
    "age": 23,
    "tags": [
      "consequat",
      "officia",
      "consectetur",
      "fugiat"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Brooke Smith"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Hodges Nielsen"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Lesa Hall"
      }
    ],
    "greeting": "Hello, undefined! You have 3 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "5d14e866db71274862f509be",
    "age": 32,
    "tags": [
      "aute",
      "officia",
      "esse",
      "voluptate"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Rivers Anderson"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Ingram Mccall"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Melton Quinn"
      }
    ],
    "greeting": "Hello, undefined! You have 9 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "5d14e866ceeadef742940bf0",
    "age": 27,
    "tags": [
      "mollit",
      "laboris",
      "consequat",
      "nisi"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Delia Woodward"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Kristin Riley"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Floyd Lowe"
      }
    ],
    "greeting": "Hello, undefined! You have 4 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "5d14e866de69ec724cea8da4",
    "age": 30,
    "tags": [
      "velit",
      "fugiat",
      "aute",
      "deserunt"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Osborn Hubbard"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Dianna Daugherty"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Sims Guy"
      }
    ],
    "greeting": "Hello, undefined! You have 10 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "5d14e86643a18516e353146a",
    "age": 34,
    "tags": [
      "id",
      "veniam",
      "voluptate",
      "esse"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Solis Nolan"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Maricela Colon"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Gilda Ortiz"
      }
    ],
    "greeting": "Hello, undefined! You have 6 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "5d14e8662452730e42d257d4",
    "age": 23,
    "tags": [
      "velit",
      "cupidatat",
      "duis",
      "dolore"
    ],
    "friends": [
      {
        "inheritedValue": 0,
        "ExpectedValue": "Cole Roman"
      },
      {
        "inheritedValue": 1,
        "ExpectedValue": "Kimberley Carney"
      },
      {
        "inheritedValue": 2,
        "ExpectedValue": "Lowery Mcdonald"
      }
    ],
    "greeting": "Hello, undefined! You have 9 unread messages.",
    "favoriteFruit": "strawberry"
  }
];

var keys = ['inheritedValue', 'effectiveValue','ExpectedValue'];
deleteJSONProperties(jsonObj2, keys);
//console.log(JSON.stringify(jsonObj2));
console.log(jsonObj2);

关于javascript - 如何在递归解析后正确显示 JSON 对象的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796282/

相关文章:

java - 将 json 写入 String 而不是 java 中的文件

Javascript ||函数语法

javascript - 为图表融合渲染容器设置圆 Angular

javascript - 在 fineuploader 中将 <ul><li> 更改为 <div>

Python dict 通过 json.loads : 到 JSON

json - 选择具有特定键值对但不具有其他键值对的文档

javascript - 有没有办法进一步压缩这段 JavaScript 代码?

javascript - while 循环的意外输出

json - AS3 和 JSON : error 1061

javascript - 将属性添加到同一索引处数组中索引处的对象