最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing empty entry from json or object - Stack Overflow

programmeradmin8浏览0评论

I am using angular 2 with a datatable to modify a large data set. After the modification I need to save the changes in my database using a spring.

this is the json that I send to the server.

{  
  "id":3,
  "mois":"2017-07-01",
  "status":"Soumise",
  "ligneNoteDeFrais":{  
    "2017-07-10":{  
      "id":1,
      "day":"2017-07-10",
      "fk":{  
        "id":2,
        "description":"frais kilomtrique",
        "nb":5,
        "pu":6,
        "totale":44,
          "typeNoteDeFrais":null
        },
        "preuves":[],
        "detailFrais":{  
          "12":{  
            "id":1,
            "description":"test",
            "nb":5,
            "pu":4,
            "totale":0,
            "typeNoteDeFrais":{  
            "id":12,
            "intitule":"gg",
            "justifiable":true,
            "justificationObligatoire":true,
            "quantifiable":true,
            "new":false
          }
        },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    },
    "2017-07-01":{  
      "fk":{ },
      "detailFrais":{  
        "12":{ },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    },
    "2017-07-02":{  
      "fk":{ },
      "detailFrais":{  
        "12":{ },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    }
  }
}

As you can see there are some empty entrie in this JSON

Is there a solution to delete these entrie before sending them to the server or so that they are not mapped in the object.

I am using angular 2 with a datatable to modify a large data set. After the modification I need to save the changes in my database using a spring.

this is the json that I send to the server.

{  
  "id":3,
  "mois":"2017-07-01",
  "status":"Soumise",
  "ligneNoteDeFrais":{  
    "2017-07-10":{  
      "id":1,
      "day":"2017-07-10",
      "fk":{  
        "id":2,
        "description":"frais kilomtrique",
        "nb":5,
        "pu":6,
        "totale":44,
          "typeNoteDeFrais":null
        },
        "preuves":[],
        "detailFrais":{  
          "12":{  
            "id":1,
            "description":"test",
            "nb":5,
            "pu":4,
            "totale":0,
            "typeNoteDeFrais":{  
            "id":12,
            "intitule":"gg",
            "justifiable":true,
            "justificationObligatoire":true,
            "quantifiable":true,
            "new":false
          }
        },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    },
    "2017-07-01":{  
      "fk":{ },
      "detailFrais":{  
        "12":{ },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    },
    "2017-07-02":{  
      "fk":{ },
      "detailFrais":{  
        "12":{ },
        "14":{ },
        "15":{ },
        "18":{ },
        "19":{ }
      }
    }
  }
}

As you can see there are some empty entrie in this JSON

Is there a solution to delete these entrie before sending them to the server or so that they are not mapped in the object.

Share Improve this question edited Jul 25, 2017 at 18:21 0mpurdy 3,3531 gold badge21 silver badges29 bronze badges asked Jul 25, 2017 at 17:43 Mohamed Amine OualiMohamed Amine Ouali 6151 gold badge9 silver badges23 bronze badges 1
  • This question is not related to angular. It is a js/ts question. – eko Commented Jul 25, 2017 at 17:48
Add a ment  | 

3 Answers 3

Reset to default 3

I adapted this from this stack overflow question's answers but because it's now typescript I think it's sufficiently different.

function removeEmpty(obj) {
  Object.keys(obj).forEach((key) => {
    if (obj[key] && typeof obj[key] === 'object') {
      const childObject = removeEmpty(obj[key]);
      if (childObject === undefined) {
        delete obj[key];
      }
    } else if (obj[key] === '' || obj[key] === null || obj[key] === undefined) {
      delete obj[key];
    }
  });
  return Object.keys(obj).length > 0 || obj instanceof Array ? obj : undefined;
};

let json = {
  "id": 3,
  "mois": "2017-07-01",
  "status": "Soumise",
  "ligneNoteDeFrais": {
    "2017-07-10": {
      "id": 1,
      "day": "2017-07-10",
      "fk": {
        "id": 2,
        "description": "frais kilomtrique",
        "nb": 5,
        "pu": 6,
        "totale": 44,
        "typeNoteDeFrais": null
      },
      "preuves": [],
      "detailFrais": {
        "12": {
          "id": 1,
          "description": "test",
          "nb": 5,
          "pu": 4,
          "totale": 0,
          "typeNoteDeFrais": {
            "id": 12,
            "intitule": "gg",
            "justifiable": true,
            "justificationObligatoire": true,
            "quantifiable": true,
            "new": false
          }
        },
        "14": {},
        "15": {},
        "18": {},
        "19": {}
      }
    },
    "2017-07-01": {
      "fk": {},
      "detailFrais": {
        "12": {},
        "14": {},
        "15": {},
        "18": {},
        "19": {}
      }
    },
    "2017-07-02": {
      "fk": {},
      "detailFrais": {
        "12": {},
        "14": {},
        "15": {},
        "18": {},
        "19": {}
      }
    }
  }
}

console.log(removeEmpty(json));

I have this inside a service which I inject wherever I need this functionality.

Note that this will delete any key that is:

  • undefined
  • null
  • empty string ''
  • empty object {}

but not empty array: []

so

{ field: 'data', field2: {}, field3: undefined, field4: null, field5: '', field6: 6, field7: [] }

will bee

{ field: 'data', field6: 6, field7: [] }

This solution is crafted specifically to object structure provided in the question

function isEmptyObject(obj) {
  return !Boolean(Object.keys(obj).length)
}

function removeEmpty(data) {
  for (let noteKey in data.ligneNoteDeFrais) {
    const note = data.ligneNoteDeFrais[noteKey]
    if (isEmptyObject(note.fk)) {
      delete note.fk
    }
    for (let detail in note.detailFrais) {
      if (isEmptyObject(note.detailFrais[detail])) {
        delete note.detailFrais[detail]
      }
    }
    if (isEmptyObject(note.detailFrais)) {
      delete note.detailFrais
    }
    if (isEmptyObject(note)) {
      delete data.ligneNoteDeFrais[noteKey]
    }
  }
}

const data = {
  "id": 3,
  "mois": "2017-07-01",
  "status": "Soumise",
  "ligneNoteDeFrais": {
    "2017-07-10": {
      "id": 1,
      "day": "2017-07-10",
      "fk": {
        "id": 2,
        "description": "frais kilomtrique",
        "nb": 5,
        "pu": 6,
        "totale": 44,
        "typeNoteDeFrais": null
      },
      "preuves": [

      ],
      "detailFrais": {
        "12": {
          "id": 1,
          "description": "test",
          "nb": 5,
          "pu": 4,
          "totale": 0,
          "typeNoteDeFrais": {
            "id": 12,
            "intitule": "gg",
            "justifiable": true,
            "justificationObligatoire": true,
            "quantifiable": true,
            "new": false
          }
        },
        "14": {

        },
        "15": {

        },
        "18": {

        },
        "19": {

        }
      }
    },
    "2017-07-01": {
      "fk": {

      },
      "detailFrais": {
        "12": {

        },
        "14": {

        },
        "15": {

        },
        "18": {

        },
        "19": {

        }
      }
    },
    "2017-07-02": {
      "fk": {

      },
      "detailFrais": {
        "12": {

        },
        "14": {

        },
        "15": {

        },
        "18": {

        },
        "19": {

        }
      }
    }

  }
}


removeEmpty(data)
console.log(data)

Thank you guys I manage to correct the solution proposed by 0mpurdy.

removeEmpty(obj) {
        Object.keys(obj).forEach((key) => {
            (obj[key] && typeof obj[key] === 'object') && (obj[key]=this.removeEmpty(obj[key]));
            (obj[key] === '' || obj[key] === null || obj[key] === undefined) && delete obj[key];
        });
        return Object.keys(obj).length > 0 ? obj : undefined;
    };

Thanks you too marzelin.

发布评论

评论列表(0)

  1. 暂无评论