Get reference to arbitrarily (deep) nested node inside JSON/JS object using value of key

Get reference to arbitrarily (deep) nested node inside JSON/JS object using value of key

Content Index :

Get reference to arbitrarily (deep) nested node inside JSON/JS object using value of key
Tag : javascript , By : akr
Date : November 28 2020, 12:01 PM

like below fixes the issue I have been looking all night on SO with lots of similar issues but none that directly solves my problem at the moment. So please have a look below. , Here's one possibility, using a for loop in a recursive function:
let data=[{id:777,name:"Level 1_section_1",children:[{id:778,name:"Level 2a",children:[]},{id:783,name:"Level 2b",children:[]}]},{id:786,name:"Level 1_section_2",children:[{id:781,name:"Level 2c",children:[]}]}];

const findNode = (arr, idToFind) => {
  for (const item of arr) {
    if (item.id === idToFind) {
      return item;
    const possibleResult = findNode(item.children, idToFind);
    if (possibleResult) {
      return possibleResult;

console.log(findNode(data, 778));

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Arbitrarily Deep Nested Pattern Matching

Tag : development , By : Felix Almeida
Date : March 29 2020, 07:55 AM
Does that help The suggested solution
There seems to be no built-in construct to pattern-test nested heads automatically. We can achieve the goal by writing a function which would, for any given (sub)expression of the form f[___]...[___], efficiently determine f (which, with a slight abuse of terminology, we may call a symbolic head for the expression). Here is the code:
SetAttributes[shead, HoldAllComplete];
shead[expr_] := Scan[Return, Unevaluated[expr], {-1}, Heads -> True];
In[105]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, x_ /; shead[x] === f]

Out[105]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
headPattern[head_] := _?(Function[Null, shead[#] === head, HoldFirst]);

In[108]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, headPattern[f]]

Out[108]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
In[110]:= m = n = 0;
g[x_] := n++;
h[x_] := m++;
{Cases[Hold[f[g[1]][h[2]]], x_ /; shead[x] === f :> Hold[x], Infinity], {m, n}}

Out[113]= {{Hold[f[g[1]][h[2]]]}, {0, 0}}
sheadEval[expr_] := Scan[Return, expr, {-1}, Heads -> True]
In[114]:= {Cases[Hold[f[g[1]][h[2]]], x_ /; sheadEval[x] === f :> Hold[x], Infinity], {m, n}}

Out[114]= {{Hold[f[g[1]][h[2]]]}, {2, 1}}

How to render an arbitrarily deep nested list?

Tag : javascript , By : n800s
Date : March 29 2020, 07:55 AM
help you fix your problem Use a recursive template, e.g. like this:
var tree = {
  subItems: [
      name: "A", 
      subItems: [ { name:"AA", subItems: [] }, { name:"AB", subItems: [] }, { name:"AC", subItems: [] } ] 
      name: "B", 
      subItems: [ { name:"BA", subItems: [] }, { name:"BB", subItems: [{name:"BB1 (etc)", subItems: []}] } ] 

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<script type="text/html" id="myTemplate">
    <ul data-bind="foreach: $data">
            <label data-bind="text: name"></label>
            <div data-bind="template: { name: 'myTemplate', data: subItems }"></div>

<div data-bind="template: { name: 'myTemplate', data: $root.subItems }"></div>

How to delete something in a JavaScript object that can be nested arbitrarily deep?

Tag : javascript , By : Noah
Date : March 29 2020, 07:55 AM
wish helps you I have a JavaScript object that manages all of my app's data. It looks something like this: , This'll do it.
remove = function(address, tree) {
    if (address.length === 1) { 
        delete tree.children[address[0]]; 
    } else {
        remove(address.slice(1), tree.children[address[0]]);

Coping with arbitrarily deep nested lists

Tag : list , By : Brian Drum
Date : March 29 2020, 07:55 AM

Flattening arbitrarily deep nested lists

Tag : python , By : semicolonth
Date : January 02 2021, 06:48 AM
Any of those help You can use a recursive generator to yield elements from nested lists:
from typing import Collection

def check_nested(obj):
    for sub_obj in obj:
        # tuples, lists, dicts, and sets are all Collections
        if isinstance(sub_obj, Collection):
            yield from check_nested(sub_obj)
            yield sub_obj

l = [[[[[1, 2]]]]]
[1, 2]

# This will work for other formats
l = [[[[[1, 2]]]], [[3, 4]]]

[1, 2, 3, 4]
Related Posts Related QUESTIONS :
  • Login Script with hidden buttons
  • Reference that lists available JavaScript events?
  • What style do you use for creating a "class"?
  • How can I enable disabled radio buttons?
  • What's the difference in closure style
  • Getting the text from a drop-down box
  • Length of a JavaScript object
  • How Do I Post and then redirect to an external URL from ASP.Net?
  • How to set up a CSS switcher
  • Wrapping lists into columns
  • How to keyboard down or up between dropdown "options"?
  • Http Auth in a Firefox 3 bookmarklet
  • How can I turn a string of HTML into a DOM object in a Firefox extension?
  • Call ASP.NET function from JavaScript?
  • JavaScript Troubleshooting Tools in Internet Explorer
  • MAC addresses in JavaScript
  • Capturing TAB key in text box
  • How to set CSS background color of HTML using JavaScript
  • How can I make the browser see CSS and Javascript changes?
  • redux how to use state
  • All data in the db is lost when closing a express based node js server
  • How to convert javascript code for Angular
  • XPath select on child nodes
  • Idle event on mouse move - how to stop script permanently on mouse move
  • Why do I get logic is not defined in this code
  • When I enter a number in the input, the first function starts
  • ES6 imports and node_modules hell
  • How to get css selector of clicked-on element?
  • How to auto input MySQL data (input column 1 data to auto input column 2 data with same column) in php?
  • Appending to Datalist Via Javascript
  • Customizing Material-UI Components with Styled-Components
  • Populate elements with class name using Javascript
  • How to execute promises in batches (first resolving 10, then next 10 and so on)
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com