DEV Community

Cover image for Cara Mudah Mengekstrak Array Ke Variabel Dengan Javascript
Aris アリス
Aris アリス

Posted on

3 3

Cara Mudah Mengekstrak Array Ke Variabel Dengan Javascript

Hai teman-teman 👋

Kali ini saya akan membagikan cara mudah untuk melakukan ekstraksi array ke variabel. Biasanya, untuk melakukan ekstraksi array kita membuat variabel dan diisi dengan mengakses indeks arraynya seperti contoh berikut:

const arr = [10, 20, 30, 40, 50];

const value1 = arr[0];
const value2 = arr[1];

console.log(value1);
console.log(value2);
Enter fullscreen mode Exit fullscreen mode

Contoh di atas sudah benar, akan tetapi dapat lebih ringkas lagi juga tidak perlu mengakses indeks satu-persatu. Berikut kode pemrogramannya:

const arr = [10, 20, 30, 40, 50];
const [value1, value2] = arr;

console.log(value1);
console.log(value2);
Enter fullscreen mode Exit fullscreen mode

Lebih ringkas bukan? Nilai dalam array akan tersimpan dalam variabel sesuai urutan indeksnya.

Demikian yang dapat saya bagikan, apabila ada yang perlu dikoreksi silahkan share di kolom komentar. 😁

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️