I am using the .data() function in jQuery to store an array as per below:
var myArray = {};
myArray[0] = {};
myArray[0][0] = "test00";
myArray[0][1] = "test01";
myArray[1] = {};
myArray[1][0] = "test10";
myArray[1][1] = "test11";
$('#datastorage').data("testname". myArray);
I want to remove only one item (myArray[0]) from the “testname” and keep the rest.
The below does not work:
$('#datastorage').removeData("testname").removeData(0);
I believe jQuery stored the array in a form of a plain object (the test $.isPlainObject() comes back true)
I am now trying to use the function .not() to remove the element…
Since the original object is an array, what’s actually stored is just a reference to the original data, so any modification you make is reflected in every reference to that array, including the one stored in
.data().So you can just remove the element from the array:
or if you want more flexibility on which elements are removed, use
.splice().or if you’ve still got access to
myArray:There’s no need to put the array back into
.data()– any of the above will modify bothmyArrayand whatever’s already in.data()– they’re the same array!.The same would apply if the data was an object, but not if it’s a primitive type.