从json或对象中删除空条目

时间:2017-07-25 17:43:02

标签: javascript typescript

我使用带有数据表的angular 2来修改大数据集。修改后,我需要使用spring保存数据库中的更改。

这是我发送给服务器的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":{ }
      }
    }
  }
}

正如您所看到的,这个JSON中有一些空的entrie

在将这些entrie发送到服务器之前是否有删除这些entrie的解决方案,或者它们未映射到对象中。

3 个答案:

答案 0 :(得分:2)

我从this stack overflow question's answers改编了这个,但因为它现在是打字稿,我认为它是完全不同的。

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

我在服务中有这个,我需要这个功能的地方注入。

请注意,这将删除以下任何键:

  • undefined
  • null
  • 空字符串''
  • 空对象{}

空数组:[]

所以

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

将成为

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

答案 1 :(得分:2)

此解决方案专门针对问题

中提供的对象结构而精心设计



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)




答案 2 :(得分:1)

谢谢你们,我设法纠正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;
    };

非常感谢marzelin。