Skip to content

Instantly share code, notes, and snippets.

@IvanofSA
Created October 15, 2019 11:41
Show Gist options
  • Save IvanofSA/41b991e4a6505f65ecdc167c7b30d1cc to your computer and use it in GitHub Desktop.
Save IvanofSA/41b991e4a6505f65ecdc167c7b30d1cc to your computer and use it in GitHub Desktop.
MyReduce.js
const tree = {
name: '/',
children:
[
{
name: 'eTc', children: [
{
name: 'config.json',
meta: {},
type: 'file'
}
],
meta: {},
type: 'directory'
},
{
name: 'hOsts',
meta: {},
type: 'file'
}
],
meta: {},
type: 'directory'
}
function check(obj, type, num) {
return obj.type === type ? num + 1 : num;
}
let myReduce = (obj, type, num) => {
let {children} = obj;
let newNum = check(obj, type, num);
if(!children) {
return newNum;
}
return children.reduce((acc, cur) => {
return myReduce(cur, type, acc)
}, newNum)
};
console.log(myReduce(tree, 'file', 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment