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

发布于 2024-11-08 11:40:36 字数 934 浏览 0 评论 0原文

<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>

我需要通过 ID 从它们中创建多维数组 我需要的元素 q4234

数组的示例是 array["local"] 我创建的["ethernet"]["port3"]["rx_flow"]="q4234"

函数是:

    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;
    }

而且我不知道如何制作函数 make_obj()

<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>

array I need is 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-11-15 11:40:36

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

此代码段将采用两个字符串(基本上是 idinnerHTML,这里是 ss2)并构造一个嵌套的对象(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;
}

它使用递归来实现其目标。如果您对代码有任何疑问,请询问。

在这里您可以尝试一下

还需要做什么?

我猜你想将这些东西从跨度收集到一个对象中,我的示例将为每个 s / s2< 创建一个对象/代码>。如果您还有任何疑问,我很乐意为您提供帮助。

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.

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.

痴意少年 2024-11-15 11:40:36

这几乎有效(不像递归函数那么优雅)

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

缺少远程/id 中的 128,但其余部分有效。我想知道如何从小于 4 的节点获取 128

我同意它不像递归函数那样灵活,但我想看看是否可以先进行“暴力”,然后再进行 par它归结为更聪明的事情。

<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>

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文