proposal-array-unique
proposal-array-unique copied to clipboard
“index” vs. function parameter: what’s the difference?
Question about https://github.com/TechQuery/array-unique-proposal#core-features:
While
Array.prototype.unique()invoked with:
no parameter, it'll work as
[...new Set(array)];one index-key parameter (
Number,StringorSymbol), it'll get values from each array element with the key, and then deduplicates the origin array based on these values;one function parameter, it'll call this function for each array element, and then deduplicates the origin array based on these returned values.
How is 2 different from 3?
2 is an abbreviated version of 3.
"key" equals to (x) => x.key
Same for number and symbols.
I think I'd be pretty confused if I learned that shortcut pattern on unique and found it wasn't available on all the other predicate-taking array methods.
I think I'd be pretty confused if I learned that shortcut pattern on
uniqueand found it wasn't available on all the other predicate-taking array methods.
maybe we can add that
That seems like it'd be better as a separate cross-cutting proposal, if so
Agreed with @ljharb here. I think we should drop 2 for now, and possibly add it as its own proposal with all of the other array methods (particularly map).
I think unique key is very common use case and I hope it could be included in this proposal.
@hax I agree it's common; but it's already common for every array method that takes a function, not just this new one.
@ljharb I think it's possible to add key param for other array methods, but consider unique() potentially could support multiple keys ( #7 ) which unlikely fit for other methods, I suppose the key usage in unique is different than other array methods.
Anything with a predicate seems like it’d apply the same to single or multiple keys.
Maybe, but I feel arr.filter(["a", "b"]), arr.find("a"), arr.some(['a', 'b']) are confusing.
I do too, but I’d think the same about unique with an array.
in the last meeting, the committee requires to remove the key: string | symbol signature. it should be a separate proposal to add this kind of feature to all Array methods. @TechQuery
@ljharb @jridgewell
Only map() may be possible to add a String-parameter signature, for example objects.map('name'), adding a string parameter to other Array methods with a callback parameter may confuse developers:
filter(): keep the elements which has an Equal string value or Truthy key-value?findIndex(): find an element index which has an Equal string value or Truthy key-value?some(): detect an element which has an Equal string value or Truthy key-value?every(): detect every element has an Equal string value or Truthy key-value?sort(): sort elements by a string key, but Ascending or Descending?
given the similarity between .map(() => 'name') and .map('name') i'd say the same potential confusion applies anywhere - or, if not, doesn't apply anywhere.
I suggest we remove index parameter, and make non-function param throw TypeError so we could reconsider it in the future.