in Education by
I have this JSON tree view that represents a menu : var menus = [ { label : "1", items: [ { label : "1.1" }, { label : "1.2", items : [ { label : "1.2.1" }, { label : "1.2.2" } ] }, { label : "1.3" }, ] }, { label : "2" } ] I want to mutate this JSON by adding for each item a selected property. This property, a boolean, will be set to true if the label is the right one or this is the tricky part if the descendant is the right one. For instance, if I'm looking for label 1.2, all labels 1 and 1.2 will be selected. So I will get this JSON : var menus = [ { label : "1", selected : true, items: [ { label : "1.1" selected : false }, { label : "1.2", selected : true, items : [ { label : "1.2.1" selected : false }, { label : "1.2.2", selected : false } ] }, { label : "1.3", selected : false }, ] }, { label : "2", selected : false } ] the selected : false is not necessary. Lodash is OK for me;)! Any suggestions? edit : where I am ! --> https://codepen.io/anon/pen/XGoXjM?editors=0010 edit 2 : finding elements must not be based on the way I wrote the labels. The labels can be any string... Sorry... Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This solution uses a for loop to iterate recursively the menu items and their children. If an item is selected, it adds selected: true to the item and it's parents: const selectMenuItems = menus => selectedLabel => { const internal = arr => { let selected = false for(let i = 0; i < arr.length; i++) { const item = arr[i] const childrenSelected = !!item.items && internal(item.items) item.selected = childrenSelected || item.label === selectedLabel selected = selected || item.selected } return selected } internal(menus) return menus } const menus = [{"label":"1","items":[{"label":"1.1"},{"label":"1.2","items":[{"label":"1.2.1"},{"label":"1.2.2"}]},{"label":"1.3"}]},{"label":"2"}] const menuSelector = selectMenuItems(menus) const result = menuSelector('1.2') console.log(result) .as-console-wrapper { top: 0; max-height: 100% !important; } Run code snippetExpand snippet

Related questions

0 votes
    I am trying to change an image depending on what option is selected in a drop down menu. I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    write a html program to display a menu card of Hotel debri with heading with atleast 3 veg and non veg ... checkboxes and place order Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Can a chatbot be used by a restaurant to take customer orders and make menu items suggestions? A. Yes B. No...
asked Dec 23, 2022 in Technology by JackTerrance
0 votes
    the items present under the which menu change according to the selected tools Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Problem I have several tests that do the same thing in mocha. This for me, it's duplication, and is the ... ) you have dataProviders. In phpUnit a dataProvider works this way:...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I am trying to figure out how to pull only specific rows and columns using Google apps script. The ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
...