亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要使用 javascript 從結果 json 文件中刪除不必要的 json 對象

我需要使用 javascript 從結果 json 文件中刪除不必要的 json 對象

月關寶盒 2022-06-23 10:25:29
我有 10000 行的結果 json 文件。在一個數組對象中,有一些我需要刪除的不必要的 json 對象。我嘗試了很多方法,但它對我不起作用。附上json文件的那一行[  {    "product_id": "easybridge",    "errors": []  },  {    "product_id": "learningstudio",    "errors": []  },  {    "product_id": "pearsontestprep",    "errors": []  },  {    "product_id": "productization",    "errors": []  },  {    "product_id": "equella",    "errors": [      {        "property": "instance.test_ids[1]",        "message": "requires property \"maintenance\"",        "schema": {          "$id": "#/properties/test_ids/items",          ],          "properties": {            "trend": {              "$id": "#/properties/test_ids/items/properties/trend",              "examples": [                true              ]            },            "display": {              "$id": "#/properties/test_ids/items/properties/display",              "type": "boolean",              "examples": [                true              ]            },            "test_id": {              "$id": "#/properties/test_ids/items/properties/test_id",              "type": "string",            },            "test_name": {              "$id": "#/properties/test_ids/items/properties/test_name",              "type": "string",            },            "maintenance": {              "$id": "#/properties/test_ids/items/properties/maintenance",              "type": "boolean",              ]            },        "instance": {          "trend": false,          "display": false,          "test_id": "8597ae3c-e2a9-45c7-b279-bde1710681be",          "test_name": "Equella Pearsonresearch Ping Test",          "nrAlertStatus": "enabled",          "test_locations": [            {              "alert_state": false,              "location_name": "AWS_US_WEST_2",              "location_label": "Portland, OR, USA",              "included_to_health": false            }如果錯誤 json 數組不為空。我什至不需要這個 json 我如何使用 java 腳本或 java 刪除“模式”json 對象和其他不必要的 json 對象和數組,特別是“模式”json 對象。請幫忙
查看完整描述

3 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

遍歷數組,查看每個對象,然后通過復制所需的數據來創建一個新數組。


例如,我認為如果對象的錯誤數組為空,則您不關心對象,并且您schema永遠不關心:


let newJSON = [];


//Assume the json variable is the parsed JSON file you posted.

for (let element of json) {


    //Must have at least one error

    if (element.errors.length > 0) {


        //Create a new object

        let newObj = {

            "product_id" : element.product_id,

            "errors" : []

        };


        //Add each errror


        for (let error of element.errors) {


            //Only copy across what we need

            newObj.errors.push({

                "property" : error.property,

                "message" : error.message

            });

        }


        //Add object to our new array of JSON

        newJSON.push(newObj);

    }

}


//newJSON is your processed JSON output


查看完整回答
反對 回復 2022-06-23
?
浮云間

TA貢獻1829條經驗 獲得超4個贊

最簡單的解決方案可以是:


const records = [{

  "product_id": "learningstudio",

  "errors": []

},

{

  "product_id": "pearsontestprep",

  "errors": []

},

{

  "product_id": "equella",

  "errors": [{

    "property": "instance.test_ids[1]",

    "message": "requires property \"maintenance\"",

    "schema": {

      "$id": "#/properties/test_ids/items",

     }

  }]

}];


const filteredRecords = records.map((record) => {

  record.errors = record.errors.map((error) => {

    return {property: error. property, message: error.message};

  });

  return record;  

});


console.log(filteredRecords);


查看完整回答
反對 回復 2022-06-23
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

您可以使用映射和解構分配來僅捕獲所需的屬性


let json = [{"product_id": "equella", "errors": [{"property": "instance.test_ids[1]","message": "requires property \"maintenance\"",'xyz': 'not needed','useless': 'not needed',},{'xyz': 'not needed',}]},]

 

let op = json.map(({product_id,errors}) =>{ 

 let { property, message } = errors[0]

 return { product_id, errors: {property,message}}

})

 

 console.log(op)


查看完整回答
反對 回復 2022-06-23
  • 3 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號