javascript从字符串动态创建多维数组

时间:2022-08-06 21:28:45
<span id="local->ethernet->port3->rx_flow">q4234</span>
<span id="local->ethernet->port3->rx">q345</span>
<span id="local->ethernet->port1->rx_flow">128</span>
<span id="remote->id">128</span>

and I need to make multidimensional array from them by ID example from element <span id="local->ethernet->port3->rx_flow">q4234</span>

并且我需要从它们中创建多维数组,从元素q4234

array I need is array["local"]["ethernet"]["port3"]["rx_flow"]="q4234"

我需要的数组是array["local"]["ethernet"]["port3"]["rx_flow"]="q4234"

function I created is:

函数创建的是:

    function make_cfg(){
    var result=new Array();
    var x=document.getElementById(*);
    var len=x.length;
    var arr;
    for (var i=0; i<=len; i++;){
        if(x[i].id){
        if(x[i].id.indexOf("->") != -1) {
            arr=x[i].id.split("->");

            result=make_obj(result,arr);

        }
        }
    }
    return result;
    }

And I have no idea how to make function make_obj()

我不知道如何让函数make_obj()

2 个解决方案

#1


5  

I won't write the whole thing for you, I just help with the hard part a bit.

我不会为你写全部的东西,我只是帮助你解决一些困难的部分。

This snippet will take the two strings (basically id and innerHTML, here s and s2) and construct a nested object (there are no associative arrays in Javascript) out of it.

这段代码将从两个字符串(基本是id和innerHTML,这里是s和s2)中构造一个嵌套对象(Javascript中没有关联数组)。

var s='local->ethernet->port3->rx_flow',
    s2='q4234',
    a=s.split('->'),
    obj=constructObject(a, s2);

function constructObject(a, final) {
    var val=a.shift();
    var obj={};
    if (a.length>0) {
        obj[val]=constructObject(a, final);
    } else {
        obj[val]=final;
    }
    return obj;
}

It uses recursion to achieve its goal. If you have any questions about the code, please ask.

它使用递归来实现它的目标。如果您对代码有任何问题,请提出。

Here you can try it out.

在这里你可以试一试。

What is left to do?

剩下要做什么?

I guess you want to collect these things from the spans into ONE object, my example will create one object for every s / s2. If you have any further questions, I am happy to help.

我猜您想要将这些东西从span中收集到一个对象,我的示例将为每个s / s2创建一个对象。如果你还有什么问题,我很乐意帮忙。

#2


0  

this almost worked (not so elegant as a recursive function)

这几乎是可行的(不像递归函数那么优雅)

http://jsfiddle.net/mplungjan/3zhwv/

http://jsfiddle.net/mplungjan/3zhwv/

Missing the 128 from the remote/id but the rest works. I would like to figure out what to do to get the 128 from the node that is shorter than 4

从远程/id中丢失了128,但是其余的工作。我想知道如何从小于4的节点得到128

I agree it is not flexible like a recursive function, but I wanted to see if I could make a "brute force" first and then par it down to something more clever.

我同意它不像递归函数那样灵活,但是我想看看我是否可以先做一个“蛮力”,然后把它分成更聪明的部分。

<span id="local->ethernet->port3->rx_flow">q4234</span>
<span id="local->ethernet->port3->rx">q345</span>
<span id="local->ethernet->port1->rx_flow">128</span>
<span id="remote->id">128</span>
<hr/>

<pre>
myObject = {
  "local":{
    "ethernet":{
       "port3": {
         "rx_flow":"q4234",
         "rx":"q345"
       } 
       "port1": {
         "rx_flow":"128"
       } 
    }
  },
  "remote":{
    "id":"128"
  } 
}
</pre>

<script>
var spans = document.getElementsByTagName("span");
var myObject = {};
for (var i=0;i < spans.length;i++) {
  var id = spans[i].id;
  var parts = id.split('->');
  var val = spans[i].innerHTML
  if (parts[0]) { // Local or remote
    if (myObject[parts[0]] == null) myObject[parts[0]]={};  
    if (parts[1]) { // ethernet or id
      if (myObject[parts[0]][parts[1]] == null) myObject[parts[0]][parts[1]]=(parts.length==1)?val:{};
      if (parts[2]) { // port3 or port1 
        if (myObject[parts[0]][parts[1]][parts[2]] == null) myObject[parts[0]][parts[1]][parts[2]]=(parts.length==2)?val:{};
        if (parts[3]) { // rx_flow or rx
          myObject[parts[0]][parts[1]][parts[2]][parts[3]]=val;
        }
      }
    }
  }    
}

for (var o in myObject) { // local or remote
  document.write(o+'/');
  for (var p in myObject[o]) { // ethernet or id
    document.write(p+'/');
    for (var q in myObject[o][p]) { // ports
      document.write(q+':/');
      for (var r in myObject[o][p][q]) { // rx_flow or rx
        document.write(r+' - '+myObject[o][p][q][r]+'<br/>');
      }
    }
  }
}
</script>

#1


5  

I won't write the whole thing for you, I just help with the hard part a bit.

我不会为你写全部的东西,我只是帮助你解决一些困难的部分。

This snippet will take the two strings (basically id and innerHTML, here s and s2) and construct a nested object (there are no associative arrays in Javascript) out of it.

这段代码将从两个字符串(基本是id和innerHTML,这里是s和s2)中构造一个嵌套对象(Javascript中没有关联数组)。

var s='local->ethernet->port3->rx_flow',
    s2='q4234',
    a=s.split('->'),
    obj=constructObject(a, s2);

function constructObject(a, final) {
    var val=a.shift();
    var obj={};
    if (a.length>0) {
        obj[val]=constructObject(a, final);
    } else {
        obj[val]=final;
    }
    return obj;
}

It uses recursion to achieve its goal. If you have any questions about the code, please ask.

它使用递归来实现它的目标。如果您对代码有任何问题,请提出。

Here you can try it out.

在这里你可以试一试。

What is left to do?

剩下要做什么?

I guess you want to collect these things from the spans into ONE object, my example will create one object for every s / s2. If you have any further questions, I am happy to help.

我猜您想要将这些东西从span中收集到一个对象,我的示例将为每个s / s2创建一个对象。如果你还有什么问题,我很乐意帮忙。

#2


0  

this almost worked (not so elegant as a recursive function)

这几乎是可行的(不像递归函数那么优雅)

http://jsfiddle.net/mplungjan/3zhwv/

http://jsfiddle.net/mplungjan/3zhwv/

Missing the 128 from the remote/id but the rest works. I would like to figure out what to do to get the 128 from the node that is shorter than 4

从远程/id中丢失了128,但是其余的工作。我想知道如何从小于4的节点得到128

I agree it is not flexible like a recursive function, but I wanted to see if I could make a "brute force" first and then par it down to something more clever.

我同意它不像递归函数那样灵活,但是我想看看我是否可以先做一个“蛮力”,然后把它分成更聪明的部分。

<span id="local->ethernet->port3->rx_flow">q4234</span>
<span id="local->ethernet->port3->rx">q345</span>
<span id="local->ethernet->port1->rx_flow">128</span>
<span id="remote->id">128</span>
<hr/>

<pre>
myObject = {
  "local":{
    "ethernet":{
       "port3": {
         "rx_flow":"q4234",
         "rx":"q345"
       } 
       "port1": {
         "rx_flow":"128"
       } 
    }
  },
  "remote":{
    "id":"128"
  } 
}
</pre>

<script>
var spans = document.getElementsByTagName("span");
var myObject = {};
for (var i=0;i < spans.length;i++) {
  var id = spans[i].id;
  var parts = id.split('->');
  var val = spans[i].innerHTML
  if (parts[0]) { // Local or remote
    if (myObject[parts[0]] == null) myObject[parts[0]]={};  
    if (parts[1]) { // ethernet or id
      if (myObject[parts[0]][parts[1]] == null) myObject[parts[0]][parts[1]]=(parts.length==1)?val:{};
      if (parts[2]) { // port3 or port1 
        if (myObject[parts[0]][parts[1]][parts[2]] == null) myObject[parts[0]][parts[1]][parts[2]]=(parts.length==2)?val:{};
        if (parts[3]) { // rx_flow or rx
          myObject[parts[0]][parts[1]][parts[2]][parts[3]]=val;
        }
      }
    }
  }    
}

for (var o in myObject) { // local or remote
  document.write(o+'/');
  for (var p in myObject[o]) { // ethernet or id
    document.write(p+'/');
    for (var q in myObject[o][p]) { // ports
      document.write(q+':/');
      for (var r in myObject[o][p][q]) { // rx_flow or rx
        document.write(r+' - '+myObject[o][p][q][r]+'<br/>');
      }
    }
  }
}
</script>