简体   繁体   中英

How to remove keys and values of JSON?

I'm using Spring Framework and angular JS in Javascript. and AJAX request succeeded. but I had a problem what removing some keys and values. my code is

$.ajax({
    type: 'POST',       
    dataType: 'JSON',
    data:  JSON.stringify(search),
    contentType:"application/json; charset=UTF-8",
    url: '/yboard/select',
    error: function() {         
        alert("Loading failed!");
    },
    success: function(returnJSON) {     

        if (returnJSON.success) {           

            var result = JSON.stringify(returnJSON.items);
            console.log("no : " + result);      


        } else {
            alert("it's failed");                           
        }
    }
});

output is

no :     [{"boardID":"9b5199799c908e48051e2e131f2d35cc","no":204,"capital_stock":"","pno_stock":"3204000336","pname_stock":"HEATER","storage_code_stock":"C03","storage_name_stock":"A","price_indicator_stock":"M","unit_stock":"EA","stock_amount_stock":"12.00","tracking_no_stock":"015","standard_stock":"WATLOW: SFRE","client_code_stock":"1193","client_name_stock":"aaa","priority_stock":0},{"boardID":"6a11d21aa400ff6c94d7d7a21b762433","no":203,"capital_stock":"","pno_stock":"3204000328","pname_stock":"HEATER","storage_code_stock":"C03","storage_name_stock":"A","price_indicator_stock":"M","unit_stock":"EA","stock_amount_stock":"12.00","tracking_no_stock":"015","standard_stock":"SFRE","client_code_stock":"1153","client_name_stock":"bbb","priority_stock":0}]

I tried to remove 'boardID' and 'priority_stock' keys by this code.

delete returnJSON.items['boardID']
delete returnJSON.items['priority_stock']

or

delete result['boardID']
delete result['priority_stock']

but it failed to remove . what's the problem?

The items property of your returnJSON object is an array. You have two items, so if you want to remove, say, boardID and priority_stock from both, you'll need to loop over your items and remove each key individually, like so:

for (var i = 0; i < returnJSON.items.length; i++) {
     delete returnJSON.items[i].boardID;
     delete returnJSON.items[i].priority_stock;
}

Delete keys for all the items

returnJSON.items.forEach(function(x){ delete x['boardID'] });
returnJSON.items.forEach(function(x){ delete x['priority_stock'] });

You can use ES6 Array Map method with Arrow function expression.

Working Demo

 let returnJSON= { "items": [{ "boardID": "9b5199799c908e48051e2e131f2d35cc", "no": 204, "capital_stock": "", "pno_stock": "3204000336", "pname_stock": "HEATER", "storage_code_stock": "C03", "storage_name_stock": "A", "price_indicator_stock": "M", "unit_stock": "EA", "stock_amount_stock": "12.00", "tracking_no_stock": "015", "standard_stock": "WATLOW: SFRE", "client_code_stock": "1193", "client_name_stock": "aaa", "priority_stock": 0 }, { "boardID": "6a11d21aa400ff6c94d7d7a21b762433", "no": 203, "capital_stock": "", "pno_stock": "3204000328", "pname_stock": "HEATER", "storage_code_stock": "C03", "storage_name_stock": "A", "price_indicator_stock": "M", "unit_stock": "EA", "stock_amount_stock": "12.00", "tracking_no_stock": "015", "standard_stock": "SFRE", "client_code_stock": "1153", "client_name_stock": "bbb", "priority_stock": 0 }] }; var res = returnJSON.items.map(obj => { (obj.hasOwnProperty('boardID')) ? delete obj.boardID : ''; (obj.hasOwnProperty('priority_stock')) ? delete obj.priority_stock : ''; return obj; }); console.log(res); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM