Skip to content

Instantly share code, notes, and snippets.

@qzm
Created November 29, 2018 08:23
Show Gist options
  • Save qzm/f6dff4ca074753d41d0b7e331e06bc38 to your computer and use it in GitHub Desktop.
Save qzm/f6dff4ca074753d41d0b7e331e06bc38 to your computer and use it in GitHub Desktop.
扁平化对象数组
/**
* 扁平化对象数组
*
* @param {Number} orginArray 原始数组
* @param {Number} childrenIndex 子数组的index
* @param {Number} removeChildren 是否删除children
* @return {Array} 数组
*/
export const flattenObjectListDeep = (orginArray, childrenIndex, removeChildren = true) => {
const flatArray = [];
function flat(array) {
array.forEach(item => {
if (removeChildren) {
flatArray.push({
...item,
[childrenIndex]: undefined,
});
} else {
flatArray.push(item);
}
if (Array.isArray(item[childrenIndex])) {
flat(item[childrenIndex]);
}
});
}
flat(orginArray);
return flatArray;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment