Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 给定一个对象数组,如何按深度n中的属性对对象进行排序_Javascript_Sorting - Fatal编程技术网

Javascript 给定一个对象数组,如何按深度n中的属性对对象进行排序

Javascript 给定一个对象数组,如何按深度n中的属性对对象进行排序,javascript,sorting,Javascript,Sorting,我有一个对象数组,我希望通过一些分组属性数据对其进行排序,还有一个字符串告诉我分组依据哪个属性(例如:“Organization”或“Organization.Name”) 我需要编写一个函数,它接收看起来像beforeData的数据,并返回afterData 输入: beforeData = [ {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName':

我有一个对象数组,我希望通过一些分组属性数据对其进行排序,还有一个字符串告诉我分组依据哪个属性(例如:“Organization”或“Organization.Name”)

我需要编写一个函数,它接收看起来像
beforeData
的数据,并返回
afterData

输入:

beforeData = [
{'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]},
{'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':14, 'LongName': 'Group C'}]},
{'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
{'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]}
]
输出:

    afterData = [
    {   
        'Group': 'Group A', 
        'entities':[
            {'name':'Adrian P', 'Id':4, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
            {'name':'Kristine Bell', 'Id':3, 'Organizations':[{'Id':12, 'LongName': 'Group A'}]},
            {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]}]
    },
    {   
        'Group': 'Group B', 
        'entities':[
            {'name':'John Doe', 'Id':1, 'Organizations':[{'Id':12, 'LongName': 'Group A'},{'Id':13, 'LongName': 'Group B'}]},
            {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},]
    },
    {   
        'Group': 'Group C', 
        'entities':[
            {'name':'FooBar', 'Id':2, 'Organizations':[{'Id':13, 'LongName': 'Group B'},{'Id':13, 'LongName': 'Group C'}]},]
    }
]
我将如何着手实现这一点?我目前的尝试非常臃肿,而且由于数据量很大,需要花费很长时间

特殊踢球者:解决此问题的函数需要能够解决此问题,而无需事先知道“按属性分组”的深度是1还是2(例如:“Organization”或“Organization.LongName”)。

来自我的信息:

// this function performs data extraction from an object
// the first argument is a name of the property to be extracted
// it might be just a 1st level deep value like `name`
// or nested like `foo.bar.baz`
// in case if one of intermediate items is an array - an array of
// results is returned
function dot(name, obj) {
    if (!name) {
        return obj;
    }

    var match = name.match(/^([^.]+)(?:\.(.*))?$/),
        head = match[1],
        tail = match[2];

    if (Array.isArray(obj)) {
        return obj.map(function(item) {
            return dot(name, item);
        });
    }

    if (obj === null || typeof obj != 'object') {
        return null;
    }

    return dot(tail, obj[head]);
}

// this function accepts an array of data and a key to group by
// as a result it returns an object with keys equal to a group by key
// and values that hold that key
function groupBy(data, key) {
    return data.reduce(function(result, item) {
        var keys = dot(key, item);
        if (!Array.isArray(keys)) {
            keys = [keys];
        }

        keys.forEach(function(key) {
            if (!(key in result)) {
                result[key] = [];
            }

            result[key].push(item);
        });

        return result;
    }, {});
}

console.log(groupBy(beforeData, 'Organizations.LongName'));
JSFiddle:

现在可以轻松地将其重新格式化为您想要的任何其他格式

例如,要从问题中获得准确的格式,这里有一个微型变压器:

function transformerExample(hash) {
    var result = [];
    for (var key in hash) if (hash.hasOwnProperty(key)) {
        result.push({
            Group: key,
            entities: hash[key]
        });
    }

    return result;
}

PS:主实现显然不能处理所有可能的错误。根据实际需求,改进并不难。

一些解释会很好。只是一个代码转储不是很有用。对于(hash中的var key)if(hash.hasOwnProperty(key)){@Mauno V:一点也不。你在这里有什么困惑?@zerkms好的,以前没有见过这样写的:)我指的是你不使用的部分{在……之后……但是在一起if@MaunoV:是的:-)根据特定的项目,代码样式可能不被接受,但如果是,这只是一种在对象上迭代的方便方式,因为我们无论如何都需要检查
hasOwnProperty
。因此,使用此表单,我将保存一个
}
,总共两行和一个额外的填充。