So it's pretty simple. I'm looking for one or two lines of JavaScript that do something useful.
I'll get the ball rolling and start with some examples:
- Shallow array clone via Array spread.
const originalArray = [1, 2, 3];
const shallowArrayClone = [...originalArray];
- Shallow array clone via
Array.protoype.slice
.
const originalArray = [1, 2, 3];
const shallowArrayClone = originalArray.slice();
- Shallow clone of an object via object spread.
const originalObject = { a:1, b: 2, c: 3 };
const shallowObjectClone = {...originalObject};
- Shallow clone of an object via object spread with one property overridden.
const originalObject = { a:1, b: 2, c: 3 };
const shallowObjectClone = {...originalObject, c: 45 };
- Get unique values of an array using
Set
const arrayWithDuplicateValues = [1, 2, 3, 3, 1, 5];
const uniqueArray = Array.from(new Set(arrayWithDuplicateValues);
or
const arrayWithDuplicateValues = [1, 2, 3, 3, 1, 5];
const uniqueArray = [...new Set(arrayWithDuplicateValues)];
- See if two arrays have the same values (unordered and for primitive values).
const a = [1, 2, 3];
const b = [2, 3, 4];
const uniques = new Set(a.concat(b));
const haveSameValues = uniques.length === a.length // or uniques.length === b.length;
- Flatten an array with the ES spread operator and Array.prototype.concat. Great tip care of Jonathan Z. White.
const arrayToFlatten = [ [1,2,3], [4,5,6], [7,8,9] ];
const flattenedArray = [].concat(...arrayToFlatten);
2020 Update for the above is
[ [1,2,3], [4,5,6], [7,8,9] ].flatMap(x=>x)
And go!
Cover image care of Flickr user Wayne Grivell.
Top comments (17)
Another one I just remembered:
It looks like the escaping only works in FireFox according to the MDN docs, RegExp.prototype.source (see Browser Compatibilty section).
Hmm. I verified that the MDN isn't just out of date by testing in Chrome. I can't remember the context I was using that snippet in precisely, but I do remember it was designed to run in IE10+, Chrome, and Firefox.
I may have just been using it so I wouldn't have to escape all my backslashes for a string.
I tried in Chrome yesterday and it doesn't error out, but it just doesn't escape.
Wow
An amazing line from @wesbos podcast syntaxfm
I've not had a chance to listen to the episode yet. Any Chance you can explain this one? Thought I had started to understand async/ await until I saw this! Thanks.
Let me break that up.
Previously it used to be,
Ahhh I see, that's amazing thanks for that! <3
Thanks! I just clarified this in a tweet:
twitter.com/wesbos/status/95583181...
Thanks Wes, keep up the good work! Loving them tasty treats :P
Here's a super easy way to remove duplicates from an array (by using the definition of a Set)!
Yup that's a good one, although I already have it up top 😺 .
Oh haha, didn't see that one!
You have an array with keys and one with values and want to merge them into an object?
Quick-and-easy deferred promise (like from
q.defer()
) - handy for modal dialogs:Better than wrapping all of that code inside of a Promise constructor every time, in my opinion.
Quick unhasher (not a generic one, but it does the job)