Skip to content

Instantly share code, notes, and snippets.

@Jiang-Xuan
Last active January 11, 2018 07:00
Show Gist options
  • Save Jiang-Xuan/79511b63d5ae9da6561266bae77b3e70 to your computer and use it in GitHub Desktop.
Save Jiang-Xuan/79511b63d5ae9da6561266bae77b3e70 to your computer and use it in GitHub Desktop.

Revisions

  1. Jiang-Xuan revised this gist Jan 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flatArray.js
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ function flatArray(array, prop, decideFunc) {
    } else {
    if (decideFunc && decideFunc(item)) {
    const child = item[prop];
    const tmp = flatArray(child, prop);
    const tmp = flatArray(child, prop, decideFunc);

    result = [...result, ...tmp];
    } else {
  2. Jiang-Xuan revised this gist Jan 11, 2018. 1 changed file with 13 additions and 8 deletions.
    21 changes: 13 additions & 8 deletions flatArray.js
    Original file line number Diff line number Diff line change
    @@ -20,29 +20,34 @@ const foo = [{
    }]
    }]

    function flatArray(array, prop) {
    function flatArray(array, prop, decideFunc) {
    if (Object.prototype.toString.call(array) !== '[object Array]') {
    throw new TypeError('The type of first argument must be array');
    throw new TypeError('The type of first argument must be an array');
    }
    if (typeof prop !== 'string') {
    throw new TypeError('The type of second argument must be string');

    throw new TypeError('The type of second argument must be a string');
    }
    if (decideFunc && Object.prototype.toString.call(decideFunc) !== '[object Function]') {
    throw new TypeError('The type of third argument must be a function');
    }
    let result = [];
    array.map(item => {
    if (!(prop in item)) {
    result.push(item);
    } else {
    const child = item[prop];
    const tmp = flatArray(child, prop);
    if (decideFunc && decideFunc(item)) {
    const child = item[prop];
    const tmp = flatArray(child, prop);

    result = [...result, ...tmp];
    result = [...result, ...tmp];
    } else {
    result.push(item);
    }
    }
    });

    return result;
    }

    flatArray(foo, 'children')

    /*
  3. Jiang-Xuan created this gist Jan 11, 2018.
    62 changes: 62 additions & 0 deletions flatArray.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    const foo = [{
    key: 'first'
    }, {
    key: 'second'
    }, {
    key: 'third'
    }, {
    key: 'more',
    children: [{
    key: 'fourth'
    }, {
    key: 'fifth'
    }, {
    key: 'more',
    children: [{
    key: 'sixth'
    }, {
    key: 'seventh'
    }]
    }]
    }]

    function flatArray(array, prop) {
    if (Object.prototype.toString.call(array) !== '[object Array]') {
    throw new TypeError('The type of first argument must be array');
    }
    if (typeof prop !== 'string') {
    throw new TypeError('The type of second argument must be string');

    }
    let result = [];
    array.map(item => {
    if (!(prop in item)) {
    result.push(item);
    } else {
    const child = item[prop];
    const tmp = flatArray(child, prop);

    result = [...result, ...tmp];
    }
    });

    return result;
    }

    flatArray(foo, 'children')

    /*
    [{
    key: 'first'
    }, {
    key: 'second'
    }, {
    key: 'third'
    }, {
    key: 'fourth'
    }, {
    key: 'fifth'
    }, {
    key: 'seventh'
    }]
    */