admin管理员组文章数量:1291721
I'm having trouble deleting/removing an item from an Array in jQuery. I've run the results in console.log() and it shows up as an Object. I've created a function which returns a json string and then I parses it, an example below:
var ret = jQuery.parseJSON($.return_json(data));
It works nicely, however, I am running an $.each
loop which removes items from that array/object.
var old = $("element").find("li[rel=item]");
$.each(old, function(index, value) {
ret.splice($(value).attr("id"), 1);
});
Above, I am searching for elements with attribute rel = item
. The same element contains an id
which is related to the index of the function which returns the json parsed variable.
I ran Developers Tools in Google Chrome to see the error and it prints:
Uncaught TypeError: Object #<Object> has no method 'splice'
Any words of guidance will be much appreciated. Thanks.
I'm having trouble deleting/removing an item from an Array in jQuery. I've run the results in console.log() and it shows up as an Object. I've created a function which returns a json string and then I parses it, an example below:
var ret = jQuery.parseJSON($.return_json(data));
It works nicely, however, I am running an $.each
loop which removes items from that array/object.
var old = $("element").find("li[rel=item]");
$.each(old, function(index, value) {
ret.splice($(value).attr("id"), 1);
});
Above, I am searching for elements with attribute rel = item
. The same element contains an id
which is related to the index of the function which returns the json parsed variable.
I ran Developers Tools in Google Chrome to see the error and it prints:
Uncaught TypeError: Object #<Object> has no method 'splice'
Any words of guidance will be much appreciated. Thanks.
Share Improve this question edited Jul 8, 2011 at 14:42 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jul 8, 2011 at 14:28 nderjungnderjung 1,6174 gold badges17 silver badges38 bronze badges 2-
Ah, that's just me when I typed it out. It's
jQuery.parseJSON
actually. – nderjung Commented Jul 8, 2011 at 14:33 - Just looking at the error means that splice method doesn't exist on ret. Which is true if ret is not an array. I am still confused on what you are trying to do. Can provide example data and what you want the output to be. – Amir Raminfar Commented Jul 8, 2011 at 14:35
3 Answers
Reset to default 4It seems like ret
is not actually an array (and likely an object (ex: {someName: "someVal"}
) instead).
I'm also making an assumption that you mean for $(value).attr("id")
to be a string identifier like someName
in the object example above. If that is the case and you are working with an object and you do have the appropriate property identifier, then luckily there is an easier solve than splice
.
Try:
$("element").find("li[rel=item]").each(function() {
delete ret[$(this).attr("id")];
});
splice
is only a method of arrays, not objects. ret
in this case, is an object, not an array.
If you are trying to remove specific elements from an object, you can do this:
$("element").find("li[rel=item]").each(function(i,v){
delete ret[v.id];
});
ps. You can use .each
instead of $.each
.
If you really want to make the object into an array, you can simply loop through it and push the elements into an array.
var obj = {"1":"item 1", "2": "item 2", "3": "Item 3"};
var arr = [];
for(i in obj){
if(obj.hasOwnProperty(i)){
arr.push(obj[i]);
}
}
ret is the JSON object containing the array, and you can't splice it. You need to reference whatever you called the array within the object and splice that.
(Would help if you posted the code where you define the array).
本文标签: javascriptjQuery delete array indexStack Overflow
版权声明:本文标题:javascript - jQuery delete array index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741538609a2384179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论