DEV Community

Cover image for JS Hack: Array & Duplicate Values
Renato Byrro for Dashbird

Posted on

3 1

JS Hack: Array & Duplicate Values

This week's hack is about cleaning duplicate values from Arrays

The Neat Dev version:

let myArr = ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

console.log([...new Set(myArr)])

// Outputs: (7) ["h", "e", "l", "o", "w", "r", "d"]

The Ninja Dev version:

Because, why not add 15 lines of code and useless comments, right!?

let u = [], a = ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

for(z in a) {
  let t = false

  // Because a for loop is always better than Array.includes()
  for(g in u) {
    // Ninjas reap maximum benefit from IF statements
    if(a[z] === u[g]) // Readability matters
      t = true
  }

  // See? The more IFs, the better
  if(!t)
    u.push(j)
}

console.log(u)

// Outputs: the same, but a little more tired of typing

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
khuongduybui profile image
Duy K. Bui

Ninja code is bad - scratch that, legendary - in their readability and maintainability, not performance.

A true ninja would write

$: for(z in a) {

$$: for(g in u) {

and

break $$

Collapse
 
byrro profile image
Renato Byrro

True 😄

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay