使用下划线操作 JavaScript 对象

Manipulating javascript object with underscore

本文关键字:JavaScript 对象 操作 下划线      更新时间:2023-09-26

我有一个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 个子组"和 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",
                            }
                         ]
                      }
                   ]
                }
             ]

试试这个:

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

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

下面是一个纯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);

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

//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)

此处的工作代码