admin管理员组

文章数量:1391934

I can't seem to work out why splice isn't working correctly in this instance.

I have read countless stack overflow examples of splice and I can't seem to see an issue.

This code should basically remove index 14, from the first item(and only) in the 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); 

I can't seem to work out why splice isn't working correctly in this instance.

I have read countless stack overflow examples of splice and I can't seem to see an issue.

This code should basically remove index 14, from the first item(and only) in the 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); 
Share Improve this question edited Mar 28, 2015 at 17:19 idmean 14.9k9 gold badges61 silver badges89 bronze badges asked Mar 28, 2015 at 17:16 jdawgjdawg 5582 gold badges5 silver badges20 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

It does not work because splice is a method available on arrays, not on objects.

And this is an 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 you get an error like:

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

You can use delete instead or convert it to an array:

delete product_variations[0]["14"]

To convert it to an array you could try:

function objectToArray(p){
    var keys = Object.keys(p);
    keys.sort(function(a, b) {
        return a - b;
    });

    var arr = [];
    for (var i = 0; i < 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); 

Use the "delete" keyword in Javascript.

delete myArray["lastname"];

As mentioned, it does not work because your object is just a list and what you are using is a object (assoc array)

.splice

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Note: This method changes the original array.

delete

The delete operator removes a property from an object.

So your code should look like

delete product_variations[0]["14"]

Remember that the 14 number is a string, not a number, as you have written in your code, since that's the name of the element in your array.

Nevertheless, I highly remend you, after having seen the code you are managing, to switch that to a list, since the keywords are only like the indices of a normal list (with the exception that they start in 1)

本文标签: javascriptJSON Parse splice issueStack Overflow