6

I have the following example code, creating a object collection.

How can I remove one of the objects? (e.g. $TestList would look as though the "delete me" item was never there. I've tried .remove, .splice, .delete etc but I'm told it's not a function.

Doing typeof($TestList) brings back object, and typeof($TestList[0]) also seems valid.

Surely I don't have to recreate the collection without one item?

(function($) { 

jQuery.QuickTest = {
    $TestList: {},
    build: function()
    {
        $TestList={};
        $TestList[0] = 
        {
            title: "part 1"
        };

        $TestList[1] = 
        {
            title: "delete me please"
        };

        $TestList[2] = 
        {
            title: "part 2"
        };

    }
}

jQuery.fn.QuickTest = jQuery.QuickTest.build;   

})(jQuery);

$(document).ready(function() {

$().QuickTest(
{
})
});

We're using jQuery 1.3.

Thanks!

4
  • Didn't $() only start working since 1.4? Besides that, could you share with us what this piece of code is supposed to do? Commented May 16, 2013 at 12:03
  • Btw, $TestList is implied global, which is a bad idea. Commented May 16, 2013 at 12:07
  • @Jack: Before 1.4, $() would return a jQuery object containing the document element. Commented May 16, 2013 at 12:31
  • @Matt Ah ... well, that makes this code even harder to understand ;-) Commented May 16, 2013 at 12:36

2 Answers 2

10

Review

First of all, it's very non-obvious what your code is supposed to do, but here are some issues:

jQuery.QuickTest = {
    $TestList: {},
    build: function()
    {
        $TestList={};

You define jQuery.QuickTest.$TestList, but inside build() you declare a global object $TestList.

Functions declared under jQuery.fn are supposed to act on a matched set of elements (referenced by this) and return it as well; your function does neither.

Answers

An answer to some of your questions:

  1. .remove() is a jQuery function that removes nodes from the DOM and needs to be called on a jQuery object.

  2. .splice() only applies to Array and even though you're accessing $TestList as if it were one, it's still just an Object.

  3. .delete() is not any function I know ;-)

Possible solution

To delete an entry from $TestList you could use the delete in this fashion:

delete $TestList[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent thanks very much! It is a completely cut down version of my code with only the absolute basics so yes, it is a bit open so sorry about that. But your explanation has given me lots to look at, so thanks very much! Quite difficult these days finding what you are trying to do when you don't really know the words to use. It was my way of doing an object available to all my other functions without having to pass it everywhere. and delete is exactly what I needed. :-)
7

use delete myObject, not myObject.delete

2 Comments

By the way this is just javascript and will work without jQuery
Will not remove the object completely. Instead, it will leave it as null.

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.