Remove value from associative array [duplicate]

Use the splice method:

var object = { 'utils': [ 'util1', 'util2' ] }

object.utils.splice(1, 1);

If you don’t know the actual position of the array element, you’d need to iterate over the array and splice the element from there. Try the following method:

for (var i = object.utils.length; i--;) {
    var index = object.utils.indexOf('util2');

    if (index === -1) break;

    if (i === index) {
        object.utils.splice(i, 1); break;
    }
}

Update: techfoobar’s answer seems to be more idiomatic than mine. Consider using his instead.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)