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
2 Answers
Reset to default 5It 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
版权声明:本文标题:javascript - JSON Parse splice issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744667118a2618599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论