javascript - Removing empty entry from json or object -
i using angular 2 datatable modify large data set. after modification need save changes in database using spring.
this json send 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 can see there empty entrie in json
is there solution delete these entrie before sending them server or not mapped in object.
this solution crafted object structure provided in question
function isemptyobject(obj) { return !boolean(object.keys(obj).length) } function removeempty(data) { (let notekey in data.lignenotedefrais) { const note = data.lignenotedefrais[notekey] if (isemptyobject(note.fk)) { delete note.fk } (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)
Comments
Post a Comment