用下划线

时间:2016-03-04 09:21:11

标签: javascript underscore.js

我有一个Javascript对象,格式如下

"items":
         {
         "Groups":[
            {
               "title":"group 1",
               "SubGroups":[
                  {
                     "title":"sub1",
                     "id" : "1",
                     "items":[
                        {
                           "title":"Ajax request 1",
                        },
                        {
                           "title":"Ajax request 2",
                        }
                     ]
                  },
                    {
                     "title":"sub2",
                     "id" : "2",
                     "items":[
                        {
                           "title":"Ajax request 3",
                        },
                        {
                           "title":"Ajax request 4",
                        }
                     ]
                  }
               ]
            }
         ]

有''组',n'子组'和n'项'。

我首先要做的是根据id获取特定组中的所有项目。这可以通过以下方式实现:

_.each(items.Groups, function(o) {
     result = _.where(o.SubGroups, {
    'id': '1'
  });
});

返回

"items":[{"title":"Ajax request 1",},{"title":"Ajax request 2",}]

然后我想获取剩余的数据,不包括我刚检索到的项目和父组。

我试过了:

_.each(items.Groups, function(o) {
        arr = _.without(o.SubGroups, _.findWhere(o.SubGroups, {id: '2'}));
  });

但这只会让我回复这样的项目:

{
 "title":"sub2",
 "id" : "2",
 "items":[{"title":"Ajax request 3"},{"title":"Ajax request 4",}]
}

而我需要的是:

 "items":
             {
             "Groups":[
                {
                   "title":"group 1",
                   "SubGroups":[
                        {
                         "title":"sub2",
                         "id" : "2",
                         "items":[
                            {
                               "title":"Ajax request 3",
                            },
                            {
                               "title":"Ajax request 4",
                            }
                         ]
                      }
                   ]
                }
             ]

3 个答案:

答案 0 :(得分:1)

试试这个:

_.each(items.Groups, function(o) {
    arr = _.without(o, _.findWhere(o.SubGroups, {id: '2'}));
});

o 应该足够了=>你想获得群组而不是子群。

答案 1 :(得分:1)

以下是纯JS实现:

JSFiddle



var data = {
  "Groups": [{
    "title": "group 1",
    "SubGroups": [{
      "title": "sub1",
      "id": "1",
      "items": [{
        "title": "Ajax request 1",
      }, {
        "title": "Ajax request 2",
      }]
    }, {
      "title": "sub2",
      "id": "2",
      "items": [{
        "title": "Ajax request 3",
      }, {
        "title": "Ajax request 4",
      }]
    }]
  }]
}

var items = [];
var group = [];

data.Groups.forEach(function(o) {
  var _tmp = JSON.parse(JSON.stringify(o));
  _tmp.SubGroups = [];
  o.SubGroups.forEach(function(s) {
    if (s.id == "1") {
      items.push(s.items);
    } else {
      _tmp.SubGroups.push(s);
      group.push(_tmp)
    }
  });
});

function printObj(label, obj) {
  document.write(label + "<pre>" + JSON.stringify(obj, 0, 4) + "</pre>")
}

printObj("group", group);
printObj("items", items);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用下划线并使用您的逻辑过滤所有子组:

//array to store subgroup with ID 1
var results = [];
var d = _.each(data.items.Groups, function(o) {
     result = _.where(o.SubGroups, {
    'id': '1'
  });
  //add to results array
  results.push(result);
});
//make a clone of the earlier object so that you get the parent structure.
var data1 = _.clone(data);
//set the filtered results to the group
data1.items.Groups = results;
//your data as you want
console.log(data1)

工作代码here