r/javascript • • Oct 12 '19

TIL — The power of JSON.stringify replacer parameter

https://pawelgrzybek.com/til-the-power-of-json-stringify-replacer-parameter/
378 Upvotes

42 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Oct 12 '19

[deleted]

2

u/TheFundamentalFlaw Oct 12 '19

Hmmm that's interesting. But as you said, u don't have all these nice array methods. In my case, I would just filter it. What about maps? When are they useful?

22

u/Speedyjens Oct 12 '19

Sets are much faster at storing unique data and checking if a value is already in the set.

5

u/[deleted] Oct 12 '19

But presumably not if you're constantly switching back and forth between sets and arrays. It's bizarre that there isn't a common "collections" API shared by at least arrays and sets (a la iterable protocol, or traits in Rust / typeclasses in Haskell).

4

u/BloodAndTsundere Oct 12 '19 edited Oct 12 '19

Agreed. Methods like reduce, filter, and map would be good additions to the Set API.

Edit: reduce would obviously be a little subtle since it depends upon the order of iteration. Maybe it would be the caller's responsibility to use a suitably commutative reduce function.

1

u/[deleted] Oct 14 '19

Someone should just create a library adding them to the Set prototype. lol

1

u/khoker Oct 12 '19

Do Sets work on Objects? As in, will it evaluate duplicate objects in some way?

1

u/Speedyjens Oct 12 '19

I believe that in es2015 it uses === to determine equality between keys

10

u/amnrzv Oct 12 '19

Maps are just like JS objects but unlike the JS object, the key doesn't have to limited to a primitive type.

5

u/PrettyWhore Oct 12 '19

Maps are basically designed to do what we use usually objects for, and keys can be any value not just strings

-2

u/leixiaotie Oct 12 '19

Huh, TIL. Usually using lodash.uniqBy for that. Hoping if the future ECMA added arr.uniq or arr.distinct natively.

2

u/Zephirdd Oct 12 '19

how do you think that uniqBy or your examples of uniq and distinct would be implemented?

simply throwing all the items in a Set is probably the best way to implement them nowadays lol

1

u/leixiaotie Oct 13 '19

Coming from C#, I'm taking their LINQ implementation. The syntax should be like lodash's uniqBy :

Array.uniq([iteratee=_.identity])

When iteratee is empty, it use Set implementation. Otherwise it use the iteratee to get the value. Pros - easier map to uniq:

products
  .map(k => ({ name: k.name, type: k.type })
  .uniq(k => k.name + k.type);

2

u/helloiamsomeone Oct 12 '19
const distinct = iterable => [...new Set(iterable)];

const uniqBy = (iterable, mapper) => {
  const result = [];
  const mapSet = new Set();
  
  for (const value of iterable) {
    const mappedValue = mapper(value);
    
    if (!mapSet.has(mappedValue)) {
      mapSet.add(mappedValue);
      result.push(value);
    }
  }
  
  return result;
};

That was easy