javascript - JSON Parse splice issue -


i can't seem work out why splice isn't working correctly in instance.

i have read countless stack overflow examples of splice , can't seem see issue.

this code should remove index 14, first item(and only) in json array.

var product_variations = json.parse('[{"0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[],"12":"","13":"","14":"red","15":"small"}]');  product_variations[0].splice(14, 1);  

it not work because splice method available on arrays, not on objects.

and object:

{"0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[],"12":"","13":"","14":"red","15":"small"} 

actually error like:

typeerror: undefined not function (evaluating 'product_variations[0].splice(14, 1)')

you can use delete instead or convert array:

delete product_variations[0]["14"] 

to convert array try:

function objecttoarray(p){     var keys = object.keys(p);     keys.sort(function(a, b) {         return - b;     });      var arr = [];     (var = 0; < keys.length; i++) {         arr.push(p[keys[i]]);     }     return arr; }  var product_variations = json.parse('[{"0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[],"12":"","13":"","14":"red","15":"small"}]');  var arr = objecttoarray(product_variations[0]);  arr.splice(14, 1);  

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -