如何基于花括号(在jquery中)将数组拆分成块?

时间:2015-12-29 05:45:54

标签: javascript jquery arrays split

我无法将此数组拆分为两个单独的块。

'\0'

3 个答案:

答案 0 :(得分:0)

使用JSON

脚本:

var json='array string'
var obj=JSON.parse(json)
var map1=obj[0]
var map2=obj[1]

现在你有map1map2大括号内的数据。

您可以像JSON一样访问数据:

var val=map1.allowChildren//will return false

或者您可以使用以下命令转换回字符串:

var str=JSON.stringify(map1)

答案 1 :(得分:0)

/ *  您可以使用以下循环来迭代对象。 * /

var js_obj = [{
    "allowChildren": false,
    "allowDrag": false,
    "allowDrop": false,
    "cls": "resource-facility-group",
    "disabled": false,
    "draggable": false,
    "editable": false,
    "expandable": false,
    "expanded": false,
    "hidden": false,
    "href": "",
    "hrefTarget": "",
    "icon": "",
    "iconCls": "resource-facility-group",
    "id": "RESFACCONT--1",
    "leaf": false,
    "onSelectFunc": "javascript:clearViewPanel()",
    "qtip": "",
    "qtipCfg": "",
    "shortName": "_facilities",
    "singleClickExpand": true,
    "target": false,
    "text": "Facilities & Classrooms"
}, {
    "allowChildren": false,
    "allowDrag": false,
    "allowDrop": false,
    "cls": "resource-contact-group",
    "disabled": false,
    "draggable": false,
    "editable": false,
    "expandable": false,
    "expanded": false,
    "hidden": false,
    "href": "",
    "hrefTarget": "",
    "icon": "",
    "iconCls": "resource-contact-group",
    "id": "RESCONCONT--1",
    "leaf": false,
    "onSelectFunc": "javascript:clearViewPanel()",
    "qtip": "",
    "qtipCfg": "",
    "shortName": "_contacts",
    "singleClickExpand": true,
    "target": false,
    "text": "Contacts"
}];
for(var i in js_obj){
    //alert(i);
  for(var j in js_obj[i]){
    console.log(js_obj[i][j]);
  }
}

答案 2 :(得分:-1)

请尝试此

var o = [{"allowChildren":false,"allowDrag":false,"allowDrop":false,"cls":"resource-facility-group","disabled":false,"draggable":false,"editable":false,"expandable":false,"expanded":false,"hidden":false,"href":"","hrefTarget":"","icon":"","iconCls":"resource-facility-group","id":"RESFACCONT--1","leaf":false,"onSelectFunc":"javascript:clearViewPanel()","qtip":"","qtipCfg":"","shortName":"_facilities","singleClickExpand":true,"target":false,"text":"Facilities & Classrooms"},{"allowChildren":false,"allowDrag":false,"allowDrop":false,"cls":"resource-contact-group","disabled":false,"draggable":false,"editable":false,"expandable":false,"expanded":false,"hidden":false,"href":"","hrefTarget":"","icon":"","iconCls":"resource-contact-group","id":"RESCONCONT--1","leaf":false,"onSelectFunc":"javascript:clearViewPanel()","qtip":"","qtipCfg":"","shortName":"_contacts","singleClickExpand":true,"target":false,"text":"Contacts"}];

var arr = $.map(o, function(el) { return el; })

Fiddle DEMO