English 中文(简体)
反射反射物体
原标题:rearrange javascript object
  • 时间:2012-05-27 19:39:26
  •  标签:
  • javascript

我最多有3个深度的物体:

OBJ.T.V.E={...}

T and V are integers (like in OBJ[0][1][ String ]={...} ) so a typical structure could be:

OBJ[0][0][ S1 ]={..}
OBJ[0][0][ S2 ]={..}
OBJ[0][1][ S1 ]={..}
OBJ[0][2][ S1 ]={..}

我想重新排列 并获得 不知怎的这个:

OBJNEW[ S1 ][0][0]={...}

其数值与 OBJ[0][0][S1] 中的数值相同。

我挣扎了好几个小时却没走运 有什么想法吗?

EDIT: Right now I tried creating an array of objects like that:

OBJ2=[];
$.each(OBJ, function(name, value) {
  $(value).each(OBJ, function(name1, value1) {
    OBJ2[name1]=[];
    $(value1).each(OBJ, function(name2, value2) {
      OBJ2[name1][name2]={};
      OBJ2[name1][name2][ S1 ]={};
    })
  })

但这一步骤失败了,因为每次...[S1]任务都覆盖了上一个对象(如S2)。

请忽略任何打字错误, 我刚重新创造了逻辑。

最佳回答

除非我误会了 否则这应该能让你走很远

function flipObject(o) {
  var i1, i2, i3, result = {};

  for ( i1 in o ) {
    if ( o.hasOwnProperty(i1) ) {
      for ( i2 in o[i1] ) {
        if ( o[i1].hasOwnProperty(i2) ) {
          for ( i3 in o[i1][i2] ) {
            if ( o[i1][i2].hasOwnProperty(i3) ) {
              if ( !(i3 in result) ) result[i3] = {};
              if ( !(i1 in result[i3]) ) result[i3][i1] = {};
              result[i3][i1][i2] = o[i1][i2][i3];
            }
          }
        }
      }
    }
  }
  return result;
}

转转转转转转

{
    "0": {
        "0": {
            "s1": "s1.0.0",
            "s2": "s2.0.0"
        },
        "1": {
            "s1": "s1.0.1",
            "s2": "s2.0.1"
        }
    },
    "1": {
        "0": {
            "s1": "s1.1.0",
            "s2": "s2.1.0"
        },
        "1": {
            "s1": "s1.1.1",
            "s2": "s2.1.1"
        }
    }
}

{
    "s1": {
        "0": {
            "0": "s1.0.0",
            "1": "s1.0.1"
        },
        "1": {
            "0": "s1.1.0",
            "1": "s1.1.1"
        }
    },
    "s2": {
        "0": {
            "0": "s2.0.0",
            "1": "s2.0.1"
        },
        "1": {
            "0": "s2.1.0",
            "1": "s2.1.1"
        }
    }
}
问题回答

您原代码中存在的一些问题是,您不能使用直线阵列 作为关联阵列 。字符串键只能用于关联阵列。下一个问题是,您可以而且应该将字符串键复制到您所有的字符串键上,而不是尝试硬编码它们。

您可以做的最简单的事情就是为您原始对象等级的每个级别写入 < code> for / < code> in in clo圈, 然后在将其写入新对象时翻转任务。 您还要检查父密钥是否存在, 如果没有, 请创建它们 。

var NEWOBJ = {};

for (var outerKey in OBJ) {
  for (var innerKey in OBJ[outerKey]) {
    for (var stringKey in OBJ[outerKey][innerKey]) {
      NEWOBJ[stringKey] = NEWOBJ[stringKey] || {};
      NEWOBJ[stringKey][outerKey] = NEWOBJ[stringKey][outerKey] || {};
      NEWOBJ[stringKey][outerKey][innerKey] = OBJ[outerKey][innerKey][stringKey];
    }
  }
}

console.log(NEWOBJ);​

据我所知,jQuery在这里将一事无成,帮不了你什么忙。这实际上并不比完成同样事情的本地Javascript简单得多。

请注意, 这将不做您移动的物体的深度副本 。 我相信, 如果您在 < code> NEWOBJ 中更改对象, 它们在 < code> OBJ 中也会被更改 。 我认为一般地解决这个问题很难, 您可能不得不为每个对象类型使用自定义的复制对象 。

请注意, 我不是一个 Javascript GURU 。 我知道在“ code' has Own Property 能够解决的物体中, 当对属性进行迭接时有时会遇到问题, 这个问题可能会在类似的例子中出现。 我不知道我头顶上有什么问题。 例如, 我用一个简单的 JSON 对象尝试了这个代码, 它起作用了 :

http://jsfidle.net/e9uST/" rel="no follow" >http://jsfidle.net/e9uST/ - 使用浏览器中的 Javascript 控制台查看输出 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签