Skip to content

Instantly share code, notes, and snippets.

@xadamxk
Created November 21, 2019 18:18
Show Gist options
  • Save xadamxk/92693c916309c7ea46cec5398153a8ae to your computer and use it in GitHub Desktop.
Save xadamxk/92693c916309c7ea46cec5398153a8ae to your computer and use it in GitHub Desktop.
Async Promise Snippet
Promise.all(elementJson.map(async (test) => {
return getTestElementValue(testId, test.id);
})).then(array => {
groupArrayByProperty(elementJson.map(function (x, i) {
x.element = array[i];
return x;
}), 'path', 0, testId, stateKey, self, testInfo)
});
@xadamxk
Copy link
Author

xadamxk commented Nov 21, 2019

function groupArrayByProperty(array, prop, index, testID, stateKey, self, testInfo) {
    // https://stackoverflow.com/a/14696535
    var grouped = {};
    for (var i = 0; i < array.length; i++) {
        if (array[i][prop]) {
            var p = array[i][prop][index];
            if (!grouped[p]) { grouped[p] = []; }
            grouped[p].push(array[i]);
        }
    }
    self.setState({
        [stateKey]: grouped,
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment