JQuery remove object from object collection

I have the following code example creating a collection of objects.

How to delete one of the objects? (for example, $ TestList would look like the “delete me” element was never there. I tried .remove, .splice, .delete, etc., but they tell me that this is not a function.

Running typeof ($ TestList) returns an object, and typeof ($ TestList [0]) also appears to be valid.

Don't I really need to recreate a 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 are using jQuery 1.3.

Thanks!

+6
source share
2 answers

Overview

First of all, it is very unobvious what your code should do, but here are some problems:

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

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

Functions declared in jQuery.fn must act on a consistent set of elements (referenced by this ) and return it; your function does not.

The answers

The answer to some of your questions:

  • .remove() is a jQuery function that removes nodes from the DOM and must be called into a jQuery object.

  • .splice() applies only to Array , and even if you access the $TestList as if it were one, it is just Object .

  • .delete() not any function that I know; -)

Possible Solution

To remove an entry from $TestList , you can use delete as follows:

 delete $TestList[1]; 
+10
source

use delete myObject , not myObject.delete

+7
source

Source: https://habr.com/ru/post/945160/


All Articles