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

时间:2019-06-27 17:47:05

标签: 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' } ]

1 个答案:

答案 0 :(得分:1)

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

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