There is an object with attachments, you need to filter its branches by the desired value in the sheet.

Given:

let data = '[{"caption":"Настройки","commands":[{"caption":"Основные","commands":[{"caption":"Цены"},{"caption":"Типы цен"},{"caption":"Первичные документы"},{"caption":"Настройка отображения ТМЦ в первичных документах"},{"caption":"Настройка первичных документов"}]},{"caption":"Общие настройки","commands":[{"caption":"Глобальные настройки"},{"caption":"Типы процессов"},{"caption":"Тип процесса (функция)"},{"caption":"Нумераторы"},{"caption":"Тип нумератора"},{"caption":"Нумератор"},{"caption":"Календарь"},{"caption":"Тип календаря"},{"caption":"Рабочий календарь"},{"caption":"Сервисы"},{"caption":"Заполнение пустых кодов"},{"caption":"Заполнить RTClass"}]}]}]'; 

Expected

 let dataRES = '[{"caption":"Настройки","commands":[{"caption":"Основные","commands":[{"caption":"Первичные документы"}]}]}]'; 

That sketched, but for some reason does not work

 let data = '[{"caption":"Настройки","commands":[{"caption":"Основные","commands":[{"caption":"Цены"},{"caption":"Типы цен"},{"caption":"Первичные документы"},{"caption":"Настройка отображения ТМЦ в первичных документах"},{"caption":"Настройка первичных документов"}]},{"caption":"Общие настройки","commands":[{"caption":"Глобальные настройки"},{"caption":"Типы процессов"},{"caption":"Тип процесса (функция)"},{"caption":"Нумераторы"},{"caption":"Тип нумератора"},{"caption":"Нумератор"},{"caption":"Календарь"},{"caption":"Тип календаря"},{"caption":"Рабочий календарь"},{"caption":"Сервисы"},{"caption":"Заполнение пустых кодов"},{"caption":"Заполнить RTClass"}]}]}]'; function f(data, searchValue) { return data.filter((item) => { if (item.commands) { let res = f(item.commands, searchValue); if (res.length > 0) { return res } } else { if (item.caption.toLowerCase() === searchValue.toLowerCase()) { return item.caption; } } }) } let dd = JSON.parse(data); console.log('dd', dd); let newArr = f(dd, 'Первичные документы'); console.log('>>>newArr', newArr); 

  • filter Does not change the collection, it only filters it, without displaying elements that are not suitable by condition - Grundy
  • The “arr.filter (callback [, thisArg])” method is used to filter an array through a function. It creates a new array, which will include only those arr elements for which a callback (item, i, arr) will return true. Relied on it. Made bad code through map. - try09
  • why do you think that the code through the map is bad? - Grundy
  • The “arr.filter (callback [, thisArg])” method is used to filter an array through a function. - namely, for filtering , you are filtering an array with one element, you have this element matching the condition - therefore it remains in the resulting array. Everything is exactly as described. - Grundy
  • because it turned out in my opinion - not very nice (with undefined elements and empty objects). After the map, I had to run through the forEach array once again and now, I got everything I wanted. I thought you could get everything much simpler and shorter (than 2 runs). In the end I made an array of branches and a sheet ['1-light', '2-light', 'necessary sheet'] - try09

0