Как удалить нулевой массив из JSON в JQuery - PullRequest
0 голосов
/ 10 мая 2019

У меня есть массив json, я хочу удалить нулевой массив из моего json.

{
  "tblFormQuestions": [
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}

Теперь я хочу удалить весь нулевой массив [] из моего json, новый json должен быть таким. Как я могу сделать это в JQuery?

{
  "tblFormQuestions": [
    {     
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {     
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}

Ответы [ 3 ]

3 голосов
/ 10 мая 2019

Использование JSON.parse

let json = `{"tblFormQuestions":[{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":308,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QShortText","Question":"What's your name?","PlaceHolderText":"Placeholder","IsNumericOnly":true,"CharacterLimit":0,"IsRequired":false},{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[{"ID":112,"FormQuestionID":309,"OptionValue":"1 "},{"ID":113,"FormQuestionID":309,"OptionValue":" 2 "},{"ID":114,"FormQuestionID":309,"OptionValue":" 3"}],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":309,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QDropdown","Question":"DQ1","IsRequired":false},{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[{"ID":115,"FormQuestionID":310,"OptionValue":"4 "},{"ID":116,"FormQuestionID":310,"OptionValue":" 5"}],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":310,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QDropdown","Question":"DQ2","IsRequired":true}],"ID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","Title":"","CreatedTime":"2019-05-08T16:38:56.637","CreatedBy":"Admin"}`

let parsed = JSON.parse(json, (key, value) =>
  Array.isArray(value) 
    ? (value.length) ? value : undefined 
    : value
)

console.log(parsed)
1 голос
/ 10 мая 2019

Это работает

let data = {
  "tblFormQuestions": [{
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}

let result = data.tblFormQuestions.forEach(el => {

  Object.keys(el).forEach(inner => {
    // this check if the field is array ,so we not delete the empty string fields
    if (Array.isArray(el[inner]) && el[inner].length == 0) {
      delete el[inner]
    }
    
  })
})

console.log(data);
0 голосов
/ 10 мая 2019

Зацикливание на объекте, и если значение любого ключа является объектом, тогда используйте filter, чтобы удалить пустой массив.

let obj = {
  "tblFormQuestions": [{
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}
let newObj = {};
for (let keys in obj) {
  if (Array.isArray(obj[keys])) {
    newObj[keys] = obj[keys].filter(function(item) {
      return item.lenght !== 0;
    })

  }
  newObj[keys] = obj[keys]
}
console.log(newObj)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...