-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
javascript 数组项统计 #21
Labels
Comments
查看输出:JSBIN const data = [
['a1', 'b1', 'c1', 'd1'],
['a1', 'b1', 'c2', 'd2'],
['a1', 'b1', 'c2', 'd3'],
['a1', 'b2', 'c3', 'd4'],
['a2', 'b3', 'c4', 'd5'],
['a2', 'b3', 'c4', 'd6'],
['a2', 'b4', 'c5', 'd7'],
['a2', 'b4', 'c6', 'd8'],
];
function createRelation(arr) {
const relationMap = {};
arr.forEach(innerArr => {
for (let i = 1; i < innerArr.length; i++) {
const prev = innerArr[i - 1];
const children = relationMap[prev];
if (children) {
if (!children.includes(innerArr[i])) {
relationMap[prev].push(innerArr[i]);
}
} else {
relationMap[prev] = [innerArr[i]];
}
}
});
return relationMap;
}
function createTree(arr, relationMap) {
const createNode = name => {
const node = { name };
if (relationMap[name]) {
node.sub = [];
relationMap[name].forEach(child => {
node.sub.push(createNode(child));
})
}
return node;
};
const rst = [];
const firstNodes = new Set();
arr.forEach(arr => firstNodes.add(arr[0]));
for (const node of firstNodes) {
rst.push(createNode(node));
}
return rst;
}
const map = createRelation(data);
const tree = createTree(data, map);
console.log(JSON.stringify(tree)); |
Online demo (open dev-tools console to see more)(() => {
function translate (arr) {
return func(JSON.parse(JSON.stringify(arr)))
}
function func (arr) {
return Object.values(arr.reduce((accumulator, currentValue) => {
let name = currentValue.shift()
let item = accumulator[name] = accumulator[name] || (currentValue.length ? {
name,
sub: []
} : {name})
item.sub && item.sub.push(currentValue)
return accumulator
}, {})).map(item => item.sub ? {
...item,
sub: func(item.sub)
} : item)
}
console.log('data:', JSON.stringify(translate(data), null, 2))
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
给定一个数组, 数组内的每一项都是一个数组, 并且有相同的长度. 写一个函数实现下面的转换.
The text was updated successfully, but these errors were encountered: