1

I have a Javascript object with a format like below

"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",
                        }
                     ]
                  }
               ]
            }
         ]

There are n 'Groups', n 'subGroups' and n 'items'.

What I want to do firstly is get all the items from a particular group based on id. This is achieved using:

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

which returns

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

Then I want to get the rest of the data, excluding the items and parent group I have just retrieved.

I tried this:

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

But this only returns me the items like this:

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

whereas what I need is this:

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

3 Answers 3

1

Just try this:

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

o should be enough => you want to get Groups and not SubGroups.

Sign up to request clarification or add additional context in comments.

Comments

1

Following is a pure JS implementation:

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

Comments

0

Using underscore and using your logic to filter all subgroups:

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

Working code here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.