This file contains 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
const groupBy = (array, prop) => { | |
const obj = {}; | |
for (let el of array) { | |
// el[prop] in obj проверяеть еще и prototype | |
// Object.prototype.hasOwnProperty.call(obj, 'key') вот самый safe вариант | |
// if Array.isArray(obj[key]) как вариант | |
if (!(obj.hasOwnProperty(el[prop]))) { | |
obj[el[prop]] = []; | |
} |
This file contains 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
. |