Dealing with a JSON Object, I am faced with a challenge where deleting elements results in undefined entries, causing issues in my usage within datatables.
Here is my current approach:
function dTable(presId){
var allregos = '[{"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"},{"presId": "a09N0000004UbBnIAK","name": "test catty","id": "001N000000O98IoIAJ"},{"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"}]';
var newJson;
var regoValue;
for (var ke in allregos) {
if (allregos.hasOwnProperty(ke)) {
regoValue = allregos[ke].presId;
if(regoValue != presId){
delete allregos[ke];
}else{
//Instead of delete, considering adding the node to a new JSON
//But facing challenges in doing so
//newJson = newJson.allregos[ke];
}
}
}
console.log(allregos);
console.log(newJson);
j$('#myRegos').dataTable( {
"data": newJson,
"destroy": true,
"columns": [
{ "title":"Name", "mData": "name", "class": "center" },
]
} );
}
Upon logging, the result includes undefined entries:
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, Object { presId="a09N0000004W3YLIA0", name="name 1", id="001N000000Mw7knIAB"}, Object { presId="a09N0000004W3YLIA0", name="Call Centre", id="001N000000MvDaMIAV"}, Object { presId="a09N0000004W3YLIA0", name="Who Is", id="001N000000MvIiaIAF"}]
Any solutions to remove these undefined elements?
Another approach I considered was adding appropriate elements to a new JSON instead of deleting. But facing challenges, for example:
newJson = newJson.allregos[ke];