2

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];
 });
});
Charles
51.5k13 gold badges107 silver badges146 bronze badges
asked Jun 21, 2013 at 7:33
2
  • I'm missing a if (file_data.file == "9Ru..."), or do you want to delete everything? Commented Jun 21, 2013 at 8:23
  • I want to delete only one file, so you are right that I am missing this condition... Commented Jun 21, 2013 at 9:47

2 Answers 2

2

Use splice:

files[index_folder].splice(index_file, 1);
answered Jun 21, 2013 at 7:40
Sign up to request clarification or add additional context in comments.

Comments

2

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']
answered Jun 21, 2013 at 7:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.