3

I have a javascript object as follow:

obj = {
   a: 'a',
   b: 'b',
}

I add obj to an array as follow:

arr = [];
arr.push(obj);

Now I want to delete arr[0]. I only access to obj, but I want to delete obj then delete automatically arr[0].

How can I do it, Or Is it possible?

6
  • 1
    Possible duplicate of stackoverflow.com/questions/5767325/… Commented Sep 5, 2015 at 9:38
  • 1
    What do you mean that only access to obj? Commented Sep 5, 2015 at 9:39
  • 1
    @Okx It's not a duplicate. I think the OP means that at the time he wants to remove obj from the array, the array has gone out of scope, which is not a situation that is answered by the question you linked to. Commented Sep 5, 2015 at 9:52
  • If you just want to remove the obj content from last array just write arr.pop() Commented Sep 5, 2015 at 9:53
  • 1
    The OP most probably wants to edit the array indirectly, by deleting obj. Not possible, see this answer. Commented Sep 5, 2015 at 10:09

3 Answers 3

1

Save the index at which your object was inserted:

arr.push(obj);
var index = arr.length - 1;

and then add a method to the object to remove it from the array, using the saved index:

obj.remove = function () {
    delete arr[index];
};

Then, somewhere else in your code where arr has gone out of scope, just do

obj.remove();

Note: this will leave a hole in your array at the place where your object was, it will not reorganize the array, shifting elements left and right to fill the hole. If you don't want to leave a hole, do not use an array, instead use a linked list.

Sign up to request clarification or add additional context in comments.

Comments

1

You could attach the list to the object itself, then access the list that way in order to delete the object? This is a bit messy, ideally you'd find a way to reorganise your code, but hey these things happen! so this might help:

http://jsfiddle.net/dk79mb3x/1/

// 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);

Comments

0

It's not possible. You must access to arr and then use delete arr[0].

Comments

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.