JSFiddle - React, Tailwind, and code Playground
JavaScript
// This function, and returning the obj, is not strictly
// necessary. I am doing it to achieve a state where the obj is
// in scope, but the list is not.
function defineStuff() {
var list = [];
var obj = {
a: 'a',
b: 'b',
// These two are the useful bits!
container: list,
index: list.length
// We can only delete this once, if you try a second time, the
// index will be incorrect!
deleted: false;
};
list.push(obj);
return obj;
}
obj = defineStuff();
// Note that the list is no longer in scope
console.log(typeof list);
// But we know it has one item in it... this should log '1'
console.log(obj.container.length);
// Now we can delete via the object like this...
if (!obj.deleted)
obj.container.splice(obj.index, 1);
// (You could work around this index issue and remove the .deleted flag by
// instead searching the list for something that matches the object. If you
// have an object.key, for example, that would work well.)
// Is it now empty? This should log '0'
console.log(obj.container.length);