2014-11-21 27 views
-1

我有一个数组和其中的许多对象。我希望能够运行每个功能,在每个subpage > file节点中每files/a更换files/and。我怎样才能做到这一点与适当的每个或循环功能?我想到了这一点,这是我脑海中的结构,看起来像可怕的解决方案。为许多子节点运行每个函数的正确方法是什么?

$.each(main, function(index, value) { 
    $.each(index.subpage, function(index, value) { 
     $.each(index.files, function(index, value) { 
      value.replace("files/a", "files/and"); 
     }); 
    }); 
}); 

主对象如下所示。

{ 
    "main": [ 
     { 
      "title": "AAA", 
      "hash": "a", 
      "subpage": [ 
       { 
        "title": "b", 
        "hash": "b", 
        "subpage": [ 
         { 
          "title": "c", 
          "hash": "c", 
          "subpage": [], 
          "files": [ 
           { 
            "files/a/b/c/01_clandestino_dev%20%282%29.jpg": {} 
           }, 
           { 
            "files/a/b/c/01_clandestino_dev%20%283%29.jpg": {} 
           } 
          ], 
          "content": "", 
          "layout": "standart" 
         } 
        ], 
        "files": [ 
         { 
          "files/a/b/01_clandestino_dev%20%282%29.jpg": {} 
         }, 
         { 
          "files/a/b/01_clandestino_dev%20%283%29.jpg": {} 
         } 
        ], 
        "content": "asd123", 
        "layout": "standart" 
       } 
      ], 
      "files": [ 
       { 
        "files/a/01_clandestino_dev.jpg": {} 
       }, 
       { 
        "files/a/01.Creative_Collective_Effect_Overview.jpg": {} 
       }, 
       { 
        "files/a/01.Bor%C3%A5s_H%C3%B6gskola_Website_Narrow.jpg": {} 
       } 
      ], 
      "content": "AAAb", 
      "layout": "standart", 
      "menuItem": "true" 
     } 
    ] 
} 
+1

可能欺骗http://stackoverflow.com/questions/2203958/jquery-recursive-iteration-over-objects – DLeh 2014-11-21 21:24:03

+1

数据结构似乎很奇怪。您正试图修改值,但所有文件名都显示为对象键不是值 – charlietfl 2014-11-21 21:30:46

+1

我想我必须在添加重命名之前删除文件名。 – kastelli 2014-11-21 21:37:59

回答

1

递归是唯一的解决方案。你需要编写一个处理一个“页”功能:

  • 过程中使用递归
  • 过程中的当前页面

每个文件中的每个文件的当前页内的每个子页面是与对象一把钥匙;您需要添加新密钥并使用delete运算符删除旧密钥。

var o = { 
 
    "main": [{ 
 
     "title": "AAA", 
 
     "hash": "a", 
 
     "subpage": [{ 
 
      "title": "b", 
 
      "hash": "b", 
 
      "subpage": [{ 
 
       "title": "c", 
 
       "hash": "c", 
 
       "subpage": [], 
 
       "files": [{ 
 
        "files/a/b/c/01_clandestino_dev%20%282%29.jpg": {} 
 
       }, { 
 
        "files/a/b/c/01_clandestino_dev%20%283%29.jpg": {} 
 
       }], 
 
       "content": "", 
 
       "layout": "standart" 
 
      }], 
 
      "files": [{ 
 
       "files/a/b/01_clandestino_dev%20%282%29.jpg": {} 
 
      }, { 
 
       "files/a/b/01_clandestino_dev%20%283%29.jpg": {} 
 
      }], 
 
      "content": "asd123", 
 
      "layout": "standart" 
 
     }], 
 
     "files": [{ 
 
      "files/a/01_clandestino_dev.jpg": {} 
 
     }, { 
 
      "files/a/01.Creative_Collective_Effect_Overview.jpg": {} 
 
     }, { 
 
      "files/a/01.Bor%C3%A5s_H%C3%B6gskola_Website_Narrow.jpg": {} 
 
     }], 
 
     "content": "AAAb", 
 
     "layout": "standart", 
 
     "menuItem": "true" 
 
    }] 
 
}; 
 

 
function process_page(page) { 
 
    if (page.main || page.subpage) { 
 
     $.each(page.main || page.subpage, function(i, subpage) { 
 
      process_page(subpage); 
 
     }); 
 
    } 
 
    if (page.files) { 
 
     $.each(page.files, function(i, file) { 
 
      $.each(file, function(oldname, value) { 
 
       var newname = oldname.replace("files/a", "files/and"); 
 
       console.log("old: " + oldname); 
 
       console.log("new: " + newname); 
 
       file[newname] = value; 
 
       delete file[oldname]; 
 
      }); 
 
     }); 
 
    } 
 
} 
 
process_page(o); 
 
console.log(o);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

+0

奇妙的作品! – kastelli 2014-11-24 16:47:59

相关问题