DEV Community

EidorianAvi
EidorianAvi

Posted on

Some Array methods I've recently encountered

Recently I've been working on my algorithms and data structure problems which has of course led to a ton of array manipulation. As I've done so I've run into some less common and less used array methods and thought I could share a few of my favorite encounters. I'll be running through 3 array methods that are less common so I wont be doing the fan favorites such as .map() or .forEach() but ones I found useful or that I just found interesting.

Let's get started.

.some()

This one falls closer to the interesting category but absolutely has its uses as well. It's very similar to .includes() but not identical.

What it does: checks if any values in an array meet a certain condition. It returns a truthy value if any pass the condition.

Example

const oddNumbers = [1, 3, 5, 7, 9, 11];

console.log(oddNumbers.some(number => number % 2 === 0);
// This will log as false.

console.log(oddNumbers.some(number => number > 8);
// This will log as true.
Enter fullscreen mode Exit fullscreen mode

This can of course be accomplished with other methods but if you're strictly lookingo see if any values in an array meet a certain condition and that's all this is the method you're looking for.

.from()

So this one is more on the interesting side than useful in my opinion simply because I feel like I would rather use other methods to perform the same things. It creates an array from a string among other things and is performed on the actual Array global object not on an array.

What it does: creates an array from a string. It can also act similar to a map however in that it can transform each index in an array.

Example

const chewbaccaArray = Array.from('chewbacca');

console.log(chewbaccaArray);
// This will log ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'];

const numbers = [1, 2, 3, 4, 5]

const tenDigits = Array.from(numbers, number => number * 10);

console.log(tenDigits);
//This will log [10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode

Again my opinion on this is while it's interesting to know about it I feel like I'd rather either map an array or split a string for the same effect.

Set

Alright so I don't know if this counts as an array method to be honest but it's easily my favorite of the bunch. This one hands down wins as the most useful of the three.

What it does: It creates an array of only unique values so there are no duplicates. This isn't performed directly on an array but an array is passed into it.

Example

const badCounting = [1, 2, 3, 3, 4, 5, 5, 5, 6];

const goodCounting = [...new Set(badCounting)];

console.log(goodCounting);
//This will log [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

Pretty sweet I know.

I think this one was the most interesting and useful encounter and I'm stoked to have it in my arsenal. I can think of many times I was in need of exactly this.

Anyways those are just some methods I've run into that I thought were worth sharing. Let me know if there are any other neat or niche ones that you can use on arrays or objects I'd love to see more.

Happy Coding and Happy New Year!

Top comments (1)

Collapse
 
hassan_k_a profile image
.

great info thank you very much.