1

I have an array of objects where the key is an md5 string and the value is an object [0315778255cca8d2f773642ad1d3678f] = {a:12,b:34}. I need to remove array elements based on their key, i tried splice but failed cause i suppose the keys are non numeric. I also tried to splice by position, but that failed to, and even delete the element but we all know that it is not ideal.

I could change my implemantation from array to object but that too will have the same problems more or less i suppose.

Any ideas are welcome

1 Answer 1

2

splice is indeed about the array indexes (as the numeric properties of arrays are called), not about other properties.

To remove a property from an object (including an array), use delete:

delete theArray['0315778255cca8d2f773642ad1d3678f'];

Despite its similarity to "delete" in other languages, delete in JavaScript has nothing (directly) to do with memory management; all it does is remove a property from an object (entirely; it doesn't just set the property's value to undefined or something).

Side note: If you're only using non-numeric properties, you probably don't want an array at all. The only reason for using JavaScript's Array type is if you need the special handling it gives properties whose names are all digits, the magic length (which only relates to the numeric properties), and the stuff from Array.prototype — none of which does anything with other, non-index properties.


Re your comment below:

delete leaves an undefined in the elements place. i need to get rid of it completely

No, delete removes the property, entirely. It does not leave undefined in its place. You might be thinking it does because any time you try to retrieve a property from an object that doesn't exist, you get back undefined. E.g.:

var obj = {};            // Object with no "foo" property at all
var x = obj.foo;
console.log("x = " + x); // "x = undefined"

You can prove that the property really is removed by using hasOwnProperty or in:

var obj = {};
console.log('foo' in obj);              // "false"
console.log(obj.hasOwnProperty('foo')); // "false"

in will check the object and its prototype chain; hasOwnProperty just checks the object itself.

Coming back to delete:

var obj = {};
console.log(obj.hasOwnProperty('foo')); // "false"
obj.foo = "bar";
console.log(obj.hasOwnProperty('foo')); // "true"
delete obj.foo;
console.log(obj.hasOwnProperty('foo')); // "false"
obj.foo = "bar";
console.log(obj.hasOwnProperty('foo')); // "true"
delete obj['foo'];
console.log(obj.hasOwnProperty('foo')); // "false"

Live example | source

Note that both delete obj.foo; and delete obj['foo']; work if foo is a valid identifier. But for properties whose names aren't valid identifiers (like your md5 sums), you have to use the bracketed form with the string as I showed above (delete theArray['0315778255cca8d2f773642ad1d3678f'];).

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

2 Comments

delete leaves an undefined in the elements place. i need to get rid of it completely
@mbouclas: No, it doesn't. I've added to the answer to explain what you're seeing.

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.