简体   繁体   English

在JavaScript中获取数组的值

[英]Get value of array in javascript

I have this list of node : 我有此节点列表:

在此处输入图片说明

10, 16 , 21, 26, fils_de_10, fils_de_16, fils_de_21 are on the same level and files_de_10 is supposed to be the child of 10 in term of the structure of my project. 10, 16 , 21, 26, fils_de_10, fils_de_16, fils_de_21处于同一级别,就我的项目结构而言, files_de_10应该是10的子级。

I want to create a function checkChild(node,id) , so example when i call checkChild(_obj,10) , It will return 15,14,13,11,12 that are the child and sub child of 10 . 我想创建一个函数checkChild(node,id) ,所以当我调用checkChild(_obj,10) ,它的15,14,13,11,12将返回15,14,13,11,12这是10的子代和子代。

I have been trying to come up with a recursive function but it becomes messy. 我一直试图提出一个递归函数,但它变得混乱。

It would be good if anyone already has some similar function. 如果有人已经具有一些类似的功能,那将是很好的。

Edited : This is the json data of the node : 编辑:这是节点的json数据:

var _str = '{"10":{"0":"0","1":"DISPONIBILITES","2":"t","style":"font-weight: bold;"},"16":{"0":"0","1":"TRESORERIE NETTE","2":"t","style":"font-weight: bold;"},"21":{"0":"0","1":"COMPTES","2":"t","style":"font-weight: bold;"},"26":{"0":"0","1":"LIGNE DE CREDIT BNP/HSBC","2":"f","style":"color:black;"},"fils_de_21":{"22":{"0":"21","1":"EXASOLAR SA","2":"f","style":"color:black;"},"23":{"0":"21","1":"EXASOLAR CORP","2":"f","style":"color:black;"},"24":{"0":"21","1":"EXASOLAR SARL","2":"f","style":"color:black;"},"25":{"0":"21","1":"EXASOLAR SL","2":"f","style":"color:black;"}},"fils_de_10":{"13":{"0":"10","1":"Disponibilits France","2":"t","style":"font-weight: bold;"},"14":{"0":"10","1":"Dispo exasolar SL","2":"f","style":"color:black;"},"15":{"0":"10","1":"Dispo exasolar corp","2":"f","style":"color:black;"},"fils_de_13":{"11":{"0":"13","1":"Dispo exasolar SA","2":"f","style":"color:black;"},"12":{"0":"13","1":"Dispo exasolar sarl","2":"f","style":"color:black;"}}},"fils_de_16":{"17":{"0":"16","1":"Trso nette exasolar SA","2":"f","style":"color:black;"},"18":{"0":"16","1":"Trso nette exasolar SL","2":"f","style":"color:black;"},"19":{"0":"16","1":"Trso nette exasolar corp.","2":"f","style":"color:black;"},"20":{"0":"16","1":"Trso nette exasolar sarl","2":"f","style":"color:black;"}}}';

Not sure if what you want is something like this. 不知道您想要的是这样的东西。

What I've done is to check inside the function is the current item key is something starting with fils_de_ , if it is, then we call the function recursively. 我所做的就是检查函数内部是否是当前项目键,它以fils_de_ ,如果是,则我们递归调用该函数。 If not, we simply add the value to our list array. 如果没有,我们只需将值添加到list数组中即可。

Take a look and let me know if that works for you. 看一看,让我知道是否适合您。

Cheers! 干杯!

 var my_data = { 10: { 0: "0", 1: "blabla", 2: "t" }, fils_de_10: { 13: { 0: "10", 1: "blabla 10_13", 2: "t" }, 14: { 0: "10", 1: "blabla 10_14", 2: "t" }, 15: { 0: "10", 1: "blabla 10_15", 2: "t" }, fils_de_13: { 11: { 0: "10", 1: "blabla 10_11", 2: "t" }, 12: { 0: "10", 1: "blabla 10_12", 2: "t" } } } }; function checkChild(node,id) { var list = []; if(node['fils_de_'+id]) for (var item in node['fils_de_'+id]) if(item.match(/fils\\_de\\_(\\d+)/)) list.push(...checkChild(node['fils_de_'+id], item.split('_')[2])); else list.push(item); else for (var item in node) if(item.match(/fils\\_de\\_(\\d+)/)) list.push(...checkChild(node[item], id)); return list; } console.log(checkChild(my_data, 10)); console.log(checkChild(my_data, 13)); 

If i get it right, what you need is a simple Breadth-first tree traversing . 如果我做对了,您需要的是一个简单的广度优先的遍历树 Check out the well known algorithm - it should work. 看看众所周知的算法-它应该可以工作。

In steps you should: 在步骤中,您应该:

  1. Build the name of the node you want to examine ("fils_de_10" in you example). 构建要检查的节点的名称(示例中为“ fils_de_10”)。
  2. Traverse the tree until you find this node. 遍历树,直到找到该节点。
  3. Traverse a subtree of this node and store all leaves as your result. 遍历此节点的子树并存储所有叶子作为结果。

Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM