SoFunction
Updated on 2025-03-06

Sample code for obtaining parent node elements based on id

Sample code for obtaining parent node elements based on id

Updated: May 13, 2023 10:05:50 Author: Zhan Libiao
This article mainly introduces the JS tree structure to obtain parent node elements based on id. This article introduces it to you in detail through the example code, which has certain reference value for your study or work. Friends who need it can refer to it.

Encapsulation function

// Pass in id and tree structure dataexport function getParentTree(id, tree) {
  let arr = [] //Array to be returned  for (let i = 0; i < ; i++) {
    let item = tree[i]
    arr = []
    (item) //Save the current node id    if (id== ) {
      //Judge whether the current id is the default id      return arr //Yes, exit the loop and return data    } else {
      // Otherwise, go to the following judgment to determine whether the current node has child node data      if ( &&  > 0) {
        //Merge the data returned by the child node        arr = (getParentTree(id,  ?  : []))
        (arr)
        if ((item2 => (item2 ?  : '')).includes(id)) {
          //If the current data already contains the default node, exit the loop and return the data          return arr
        }
      }
    }
  }

Calling functions

const treeData = [{
  name: '1',
  id: 1,
  children: [{
    name: '1-1',
    id: 2,
    children: [{
      name: '1-1-1',
      id: 4,
    }],
    name: '1-2',
    id: 3,
    children: [{
      name: '1-2-1',
      id: 5,
    }],
  }]
},{
  name: '2',
  id: 6,
  children: [{
    name: '2-1',
    id: 7,
    children: [{
      name: '2-1-1',
      id: 9,
    }],
    name: '2-2',
    id: 8,
    children: [{
      name: '2-2-1',
      id: 10,
    }],
  }]
}]
(getParentTree(5, treeData))

This is the article about obtaining parent node elements based on id in JS tree structure. For more related js to obtain parent node elements, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!

  • js
  • Tree structure
  • Node Elements

Related Articles

  • Detailed explanation of JavaScript deconstruction and assignment

    This article mainly introduces JavaScript deconstruction and assignment, which has certain reference value. Interested friends can refer to it. I hope it can help you.
    2021-12-12
  • JavaScript implements dynamic loading and deleting tables

    This article mainly introduces the JavaScript dynamic loading and deletion table for you in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2021-04-04
  • A fastest server turnaround code

    JS implementation chooses the fastest server steering code
    2009-04-04
  • js to save the canvas image, or directly save an image

    Below, the editor will share with you an implementation method of saving canvas generated images or directly saving an image. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2018-01-01
  • JavaScript method to compare whether two objects are equal

    This article mainly introduces JavaScript to compare whether two objects are equal. By comparing various aspects of js objects, we can judge whether the two objects are equal. It has certain reference value. Friends who need it can refer to it.
    2015-02-02
  • JS implements the sidebar of JD product classification

    This article mainly introduces the sidebar of JS implementing JD product classification in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2020-12-12
  • A brief discussion on loop vs recursion

    The code in this article uses JavaScript. Some students' understanding of recursion is still "a method that is inefficient than loops in finding factorials." But in fact, the problems of recursion and loop processing are different. Take the "traversal array" problem as an example: a loop is suitable for traversals on the same dimension (unlimited single-layer length), while recursion is suitable for traversals across dimensions (unlimited layers).
    2013-02-02
  • Native js realize click carousel to switch pictures

    This article mainly introduces the native js click carousel to switch pictures in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2020-02-02
  • Methods to prevent high-frequency triggering and high-frequency calls of event functions in JavaScript

    This article mainly introduces the methods to prevent high-frequency triggering and high-frequency calls of event functions in JavaScript. This article is excerpted from it. Friends who need it can refer to it.
    2014-09-09
  • Detailed explanation of immediate execution of function instances in JavaScript

    Javascript is more casual compared to other programming languages, so Javascript code is full of all kinds of weird writing methods. Sometimes you can see flowers in the fog. Of course, being able to understand various types of writing methods is also a further in-depth understanding of the characteristics of Javascript language. This article mainly introduces relevant information about executing functions immediately in JavaScript. Friends who need it can refer to it.
    2017-11-11

Latest Comments