1

我正在尝试用 javascript 编写一个函数,该函数从 JSON 输入数据返回属于特定组的数据,包括该组的子组。数据如下所示:

            [
            {"id":"0", "name":"Person 0"},
            {"id":"1", "name":"Person 1","group":"0"},
            {"id":"2", "name":"Person 2","group":"0"},
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"4"},
            {"id":"6", "name":"Person 6","group":"4"},
            {"id":"7", "name":"Person 7","group":"0"},
            {"id":"8", "name":"Person 8","group":"7"}
            ]

这里,组 x 中的人与 id x 的人属于同一组。

例如:function(data, group) 将为 function(data, 2) 返回以下内容:

            [
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"2"},
            {"id":"6", "name":"Person 6","group":"2"},
            ]

和功能(数据,0):

            [
            {"id":"1", "name":"Person 1","group":"1"},
            {"id":"2", "name":"Person 2","group":"2"},
            {"id":"3", "name":"Person 3","group":"2"},
            {"id":"4", "name":"Person 4","group":"2"},
            {"id":"5", "name":"Person 5","group":"2"},
            {"id":"6", "name":"Person 6","group":"2"},
            {"id":"7", "name":"Person 7","group":"7"},
            {"id":"8", "name":"Person 8","group":"7"}
            ]

我试图循环遍历数组,但这不处理子组,所以我想我必须以递归方式来做?

4

3 回答 3

2

您可以.reduce()在子组中进行数据数组和连接。

function dataForGroup(data, group, refs) {
    if (!refs)
        refs = {};

       // check if it's in the list
    if (refs[group] === true)
        return []; // or you can throw an Error
    else
        refs[group] = true; // first encounter, so add it to the list

    return data.reduce(function(res, obj) {
        return obj.group == group ? 
                   res.concat(obj, dataForGroup(data, obj.id, refs)) : res;
    }, []);
}

希望没有循环引用!

虽然我不理解group(data, 0)输出,也没有看到任何子组数据。

于 2012-10-08T22:17:44.947 回答
1

遍历整个数据数组并过滤掉具有相同组的项目。

    var newArr = [],
    isVisted = []; // using to prevent circular reference.

function getGroup(data, groupId, inside) {
    if (!inside) {
        newArr = [];
        isVisted = [];
    }
    if (isVisted.indexOf(groupId) !== -1) {
        return
    };
    for (var k = 0; k < data.length; k++) {
        if (data[k].group == groupId) {
            newArr.push(data[k])
            getGroup(data, data[k].id, true);
        }
    }

    return newArr;
}

jsfiddle 演示

于 2012-10-08T22:17:15.310 回答
0

因为你说使用javascript,所以我只使用javascript。然而,在 jquery 中有更简单的方法来做这样的事情。我没有测试它,只是写了它,所以如果有任何问题,请不要犹豫发表评论。

function Group(data)
{
    var result = [];

    for (var item in data)
    {
        var group = (item.group) ? item.group : -1;

        if (!result[group])

            result[group] = [];

        result[group].push(item);
    }

    return result;
}

var secondGroup = Group(
    [
        {"id":"0", "name":"Person 0"}, 
        {"id":"1", "name":"Person 1","group":"0"}, 
        {"id":"2", "name":"Person 2","group":"0"}, 
        {"id":"3", "name":"Person 3","group":"2"}, 
        {"id":"4", "name":"Person 4","group":"2"}, 
        {"id":"5", "name":"Person 5","group":"4"}, 
        {"id":"6", "name":"Person 6","group":"4"}, 
        {"id":"7", "name":"Person 7","group":"0"}, 
        {"id":"8", "name":"Person 8","group":"7"} 
    ]
)[2];

干杯

于 2012-10-08T22:20:17.397 回答