Created
November 29, 2018 08:23
-
-
Save qzm/f6dff4ca074753d41d0b7e331e06bc38 to your computer and use it in GitHub Desktop.
扁平化对象数组
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 扁平化对象数组 | |
* | |
* @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