tomas657
tomas657

Reputation: 5979

How to remove item from multidimensional object by value

I need to remove item from multidimensional javascript object by value. For example, I have this objecť tree (screenshot contains only part): https://dl.dropboxusercontent.com/u/13057084/files-tree.png

enter image description here

I need to remove item by value of "file", so for example from tree on screenshot I need to remove file "9RuOxSPnTR-i_1.jpg".

I tried to use this:

$.each(files, function (index_folder,folder) { // foreach files as folders
  $.each(folder, function (index_file,file_data) { // foreach folders as files (index_file = numeric index key of file)    
    delete files[index_folder][index_file];
  });
});

Upvotes: 2

Views: 2570

Answers (2)

Andreas Louv
Andreas Louv

Reputation: 47099

You use delete to remove properties from objects and .splice to remove elements from arrays:

> var o = {a: 1, b: 2};
> delete o.a;
true
> o;
{b: 2}

> var a = ['a', 'b', 'c', 'd'];
> a.splice(2, 1);
['c']
> a;
['a', 'b', 'd']

Upvotes: 2

Lian
Lian

Reputation: 2357

Use splice:

files[index_folder].splice(index_file, 1);

Upvotes: 2

Related Questions