Cod*_*ate 6 javascript sorting
我有一个对象数组,我希望通过一些分组属性数据和一个字符串告诉我要分组的属性(例如:'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'}]}
]
Run Code Online (Sandbox Code Playgroud)
输出:
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'}]},]
}
]
Run Code Online (Sandbox Code Playgroud)
我将如何实现这一目标?我目前的尝试非常臃肿,并且在给定大量数据的情况下永远都是如此.
特别踢球者!:解决此问题的函数需要能够在不事先知道" 按属性分组 "是否为深度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'));
Run Code Online (Sandbox Code Playgroud)
JSFiddle: http: //jsfiddle.net/w8N4j/
现在可以轻松地将其重新格式化为您想要的任何其他格式。
例如,要从这里的问题中获取确切的格式,需要一个微型转换器:
function transformerExample(hash) {
var result = [];
for (var key in hash) if (hash.hasOwnProperty(key)) {
result.push({
Group: key,
entities: hash[key]
});
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
PS:主要实现显然可能无法处理所有可能的错误。根据实际需求进行改进并不难。