简体   繁体   English

更改对象JavaScript中数组的位置

[英]Change position of array in object javascript

I have an object 'test' : 我有一个对象“测试”:

var test={
   "unit3":[
      {
         "chapter":{
            "id":4,
            "name":"earth",
            "id_above":2
         }
      },
      {
         "book":{"name":"earth"}
      }
   ],
   "unit4":[
      {
         "chapter":{
            "id":5,
            "name":"water",
            "id_above":2
         }
      },
      {
         "book":{"name":"sea"}
      }
   ],
   "unit1":[
      {
         "chapter":{
            "id":2,
            "name":"biology",
            "id_above":2
         }
      },
      {
         "book":{"name":"science"}
      }
   ]
};

According to 'id_above value' (id_above and id should be same , if not, I have to move it under the right unit), I would like to get this : 根据“ id_above值”(id_above和id应该相同,如果不同,我必须将其移至正确的单位下),我想得到这个:

 var hope={
  "unit1":[
    {
      "chapter":{
        "id":2,
        "name":"biology",
        "id_above":2
      }
    },
    {
      "book":{"name":"science"}
    }
  ],
  "sub":[
    {
      "unit3":[
        {
          "chapter":{
            "id":4,
            "name":"earth",
            "id_above":2
          }
        },
        {
          "book":{"name":"earth"}
        }
      ],
      "unit4":[
        {
          "chapter":{
            "id":5,
            "name":"water",
            "id_above":2
          }
        },
        {
          "book":{"name":"sea"}
        }
      ]
    }
  ]
}

I tried : 我试过了 :

   for (var key in test){
   if(test.hasOwnProperty(key)){
   for (var i =0;i<test[key].length;i++){
   if (test[key][i].chapter.id!=test[key][i].chapter.id_above){


   for (var key2 in test){
    if(test.hasOwnProperty(key2)){
    for (var j =0;j<test[key2].length;j++){
    var o=_.find(test[key2][j].chapter,{id:test[key][i].chapter.id_above})
    console.log("o",o); //undefined
    } }}}   } } }

The problem is that my variable o is 'undefined' , I would like to do after something like: 问题是我的变量o是'undefined',我想执行以下操作:

test.sub=a;         

Could you help me please ? 请问你能帮帮我吗 ? Thanks. 谢谢。

You can't readily use JavaScript objects as ordered containers. 您不能轻易将JavaScript对象用作有序容器。 Instead, use an array. 而是使用数组。

While it's true that JavaScript object properties have an order now (or that some operations act as though object properties have an order now, depending on how you want to look at it), that's a new thing (as of ES2015) and that order is difficult to work with. 确实,JavaScript对象属性现在具有顺序(或者某些操作好像对象属性现在具有顺序,这取决于您要如何看待它),但这是新事物(从ES2015开始),并且该顺序是很难合作。 Notably, the for-in statement does not support property order, nor does Object.keys . 值得注意的是, for-in语句支持产权制度,也没有Object.keys Some new operations such as Object.getOwnPropertyNames do support the new order. 某些新操作(例如Object.getOwnPropertyNames确实支持新顺序。

When you want an order, use an array. 需要订单时,请使用数组。

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

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