2

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.

2
  • Ah, that's just me when I typed it out. It's jQuery.parseJSON actually. 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. Commented Jul 8, 2011 at 14:35

3 Answers 3

4

It 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")];
});
Sign up to request clarification or add additional context in comments.

3 Comments

As an additional suggestion... Although it may not be applicable, I would refrain from using the discussed json structure. If your keys are actually "1", "2", "3", then structuring your data as a true array ["item 1", "item 2", "item 3"] is also proper json and would likely make more sense. Also an html id should start with a letter; it is invalid to have it simply be a numeric value.
Thanks for the tips. Noted :D
The delete should work, however you could always ret[$(this).attr("id")] = null; to just clean out the values. Try to console.log($(this).attr("id")); to make sure the value is what you think expect it to be.
3

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]);
  }
}

4 Comments

Any method of conversion? jQuery.makeArray() doesn't help.
What exactly do yo want to do with the object (or array)? Remove specific elements?
Remove items from ret. I would like ret as an array so I can use splice to remove items via indexes.
@Aleks: If you want to make the object into an array, you can loop through the object and push its values into an array.
0

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).

7 Comments

There is not array in the object. Unparsed it looks like this {"1":"item 1", "2": "item 2", "3": "Item 3"}.
I don't understand you say you are having trouble splicing from an array yet it's not actually an array? You cannot splice it if it is not an array or a string.
Yes, it's an object. How does one make it an Array?
@Aleks: Loop through the elements and make it into an array. Something like for(x in obj){arr.push(obj[x])}
JSON objects can contain arrays, can you post the function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.