Skip to content

Instantly share code, notes, and snippets.

@syropian
Created April 22, 2020 14:48
Show Gist options
  • Select an option

  • Save syropian/9a3ca105d7782f7dacff245b9e51a951 to your computer and use it in GitHub Desktop.

Select an option

Save syropian/9a3ca105d7782f7dacff245b9e51a951 to your computer and use it in GitHub Desktop.

Revisions

  1. syropian created this gist Apr 22, 2020.
    19 changes: 19 additions & 0 deletions groupSiblingsByKeyValue.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    const groupSiblingsByKeyValue = (arr, k, v) => {
    return arr
    .map((o, i, a) => {
    if (o[k] === v) {
    let j = i;
    const group = [];
    while (a[j][k] === v) {
    group.push(a[j]);
    a[j] = false;
    j++;
    }

    return group.length > 1 ? group : o;
    } else {
    return o;
    }
    })
    .filter(Boolean);
    }