proposal-array-unique icon indicating copy to clipboard operation
proposal-array-unique copied to clipboard

“index” vs. function parameter: what’s the difference?

Open mathiasbynens opened this issue 5 years ago • 15 comments

Question about https://github.com/TechQuery/array-unique-proposal#core-features:

While Array.prototype.unique() invoked with:

  1. no parameter, it'll work as [...new Set(array)];

  2. one index-key parameter (Number, String or Symbol), it'll get values from each array element with the key, and then deduplicates the origin array based on these values;

  3. 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?

mathiasbynens avatar Jun 12 '20 15:06 mathiasbynens

2 is an abbreviated version of 3.

"key" equals to (x) => x.key

Same for number and symbols.

Jack-Works avatar Jun 12 '20 15:06 Jack-Works

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.

ljharb avatar Jun 12 '20 16:06 ljharb

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.

maybe we can add that

Jack-Works avatar Jun 12 '20 16:06 Jack-Works

That seems like it'd be better as a separate cross-cutting proposal, if so

ljharb avatar Jun 12 '20 18:06 ljharb

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).

jridgewell avatar Jul 09 '20 05:07 jridgewell

I think unique key is very common use case and I hope it could be included in this proposal.

hax avatar Jul 10 '20 19:07 hax

@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 avatar Jul 10 '20 20:07 ljharb

@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.

hax avatar Jul 11 '20 11:07 hax

Anything with a predicate seems like it’d apply the same to single or multiple keys.

ljharb avatar Jul 11 '20 19:07 ljharb

Maybe, but I feel arr.filter(["a", "b"]), arr.find("a"), arr.some(['a', 'b']) are confusing.

hax avatar Jul 11 '20 20:07 hax

I do too, but I’d think the same about unique with an array.

ljharb avatar Jul 11 '20 21:07 ljharb

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

Jack-Works avatar Jul 25 '20 03:07 Jack-Works

@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?

TechQuery avatar Sep 10 '20 10:09 TechQuery

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.

ljharb avatar Sep 10 '20 16:09 ljharb

I suggest we remove index parameter, and make non-function param throw TypeError so we could reconsider it in the future.

hax avatar Sep 10 '20 18:09 hax