码迷,mamicode.com
首页 > 编程语言 > 详细

数据扁平化的算法

时间:2018-05-22 13:04:26      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:orm   string   cti   ==   item   nod   []   算法   tle   

export const serialize = (data, sections = [], weeks = []) => {
const getWithParentArray = (newArr, parentIndex = -1, parentIndexObj = {}) =>
newArr.map((item, index) => {
if (item.node_lev < 2) {
let parentObj = {};
if (parentIndex > -1) {
weeks[parentIndex].push(item.title);
parentObj = { section: parentIndex, week: index };
} else if (item.node_lev === 0) {
sections.push(item.title);
weeks.push([]);
}
return getWithParentArray(item.children, index, parentObj);
}
return { ...item, week: parentIndex, ...parentIndexObj };
});
return {
sections,
weeks,
courses: getWithParentArray(data),
};
};

export const flatten = (data, arr = []) => {
data.forEach((item) => {
if (item instanceof Array) {
flatten(item, arr);
} else {
arr.push(item);
}
});
return arr;
};

/* {
"sections": [
"string"
],
"weeks": [
["string"]
],
"courses": [{
"_id": "string",
"title": "string",
"course_type": "NORMAL",
"section": 0,
"week": 0,
}]
} */

数据扁平化的算法

标签:orm   string   cti   ==   item   nod   []   算法   tle   

原文地址:https://www.cnblogs.com/zhouyideboke/p/9070647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!