{"id":257,"date":"2022-08-30T14:58:20","date_gmt":"2022-08-30T14:58:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/deleteing-json-object-from-an-array-creates-undefined-objects-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:58:20","modified_gmt":"2022-08-30T14:58:20","slug":"deleteing-json-object-from-an-array-creates-undefined-objects-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/deleteing-json-object-from-an-array-creates-undefined-objects-record-and-share-programming-errors\/","title":{"rendered":"Deleteing JSON object from an array creates &ldquo;undefined&rdquo; objects-Record and share programming errors"},"content":{"rendered":"<p>i am haveing some problems with JSON and arrays. I have been messing around with JSON for a little while and am trying to use some in production by refactoring a old implementation. What i had was two hidden textfields, one store ids in the format [1][2][3] etc and the other name [name1][name2][name3] so i thought this would be a great excercise to learn more about JSON and refactor that out and use a more readable object notation.<\/p>\n<p>Anyway i digress. The problem i am having is a funny one, i found out how to &#8220;push&#8221; JSON into an array, but the problem comes in my delete method. When i delete an object out of the array the commas persist, creating &#8220;undefined&#8221; objects. Am i doing this wrong, and is there a better way?<\/p>\n<p><strong>Added 2 items to array (all fine)<\/strong><\/p>\n<pre><code>[{id:\"1\", name:\"Test (ID: 1)\", status:\"new\"}, {id:\"2\", name:\"Test 2 (ID: 2)\", status:\"new\"}]\n<\/code><\/pre>\n<p><strong>Removed 1 item from array (commas are left in)<\/strong><\/p>\n<pre><code>[{id:\"1\", name:\"Test (ID: 1)\", status:\"new\"}, ,]\n<\/code><\/pre>\n<p><strong>Added another item back into array, commas now cause &#8220;undefined&#8221; objects<\/strong><\/p>\n<pre><code>[{id:\"1\", name:\"Test (ID: 1)\", status:\"new\"}, , {id:\"2\", name:\"Test 2 (ID: 2)\", status:\"new\"}]\n<\/code><\/pre>\n<p><strong>Here is my delete function<\/strong><\/p>\n<pre><code>    function removeFromList(Id) {\n    var txtIDs = $(\"#\");\n    var data = eval(txtIDs.val());\n\n    for (var i = 0; i &lt; data.length; i++) {\n        alert(typeof (data[i]));\n        if (typeof(data[i]) != 'undefined') {\n            if (data[i].id == Id)\n                delete data[i]; \/\/ alert(data[i].name); \/\/  \n        }\n    }     \n}\n<\/code><\/pre>\n<p>Thanks for looking at this for me \ud83d\ude42<\/p>\n<p>Rob<\/p>\n<ol>\n<li>\n<p>Delete is an operator used to delete keys from objects &#8211; you shouldn&#8217;t really use it to remove items from an Array because it won&#8217;t do what you expect. You can use Array methods like splice or slice to manipulate the array.<\/p>\n<p>The MDC Array methods documentation will be helpful \ud83d\ude42<\/p>\n<p>Alternatively, you could use a function like this:<\/p>\n<pre><code>function removeArrayEl(arr, index){\n    newArr = [];\n    for(var i = 0, l = arr.length;i &lt; l; i++){\n        if(i != index){\n            newArr.push(arr[i]);\n        }\n    }\n    return newArr;\n}\n<\/code><\/pre>\n<p>[edit] As the commenters have pointed out, the Array splice method is exactly the right tool for this job, but I&#8217;ll leave the function for reference. [\/edit]<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 06:55:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i am haveing some problems with JSON and arrays. I have been messing around with JSON for a little while and am trying to use some in production by refactoring a old implementation. What i had was two hidden textfields, one store ids in the format [1][2][3] etc and the other name [name1][name2][name3] so i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}