Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Vue

JavaScript: Update Array Immutably dengan `with()`

1 min read .
JavaScript: Update Array Immutably dengan `with()`

Kalau kamu ingin mengubah elemen array tanpa mengubah array asli, with() jawabannya.

1. Dasar with()

let newArray = array.with(index, value);
  • Membuat array baru dengan elemen di index diganti value.
  • Array asli tetap utuh.

Contoh:

const colors = ['red','blue','green','yellow'];
const updatedColors = colors.with(1,'purple');

console.log(updatedColors); // ['red','purple','green','yellow']
console.log(colors);        // ['red','blue','green','yellow']

2. Gunakan untuk Immutable Updates

const items = ['item1','item2','item3'];
const updatedItems = items.with(2,'newItem');

console.log(updatedItems); // ['item1','item2','newItem']
console.log(items);        // ['item1','item2','item3']

3. Kombinasi dengan Metode Lain

const numbers = [1,2,3,4,5];
const modifiedNumbers = numbers.with(3,100).map(num => num*2);

console.log(modifiedNumbers); // [2,4,6,200,10]
console.log(numbers);         // [1,2,3,4,5]

4. Kegunaan Real-World

  • UI Updates: Update elemen di daftar/tabel tanpa mengubah data asli.
  • State Management: Cocok untuk Redux atau sistem yang memerlukan immutability.
  • Data Processing: Non-destructive updates untuk dataset penting.

5. Catatan Performa

Membuat array baru → butuh memory tambahan, jadi perhatikan jika array besar.

Kesimpulan

with() memungkinkan update elemen array secara immutable, bikin kode lebih aman, predictable, dan selaras dengan prinsip functional programming.

Lihat Juga

JavaScript: Cara Cepat Mengelompokkan Data dengan `Object.groupBy()`
JavaScript: Cara Cepat Mengelompokkan Data dengan `Object.groupBy()`
Kadang di proyek JS, saya perlu mengelompokkan data biar lebih mudah diproses. Object.groupBy() bikin hidup lebih gampang. 1. Dasar Object.groupBy() Copy Object.groupBy(array, callback); array → data yang mau dikelompokkan callback → menentukan key tiap elemen 2. Contoh Sederhana: Genap/Ganjil Copy const numbers = [1,2,3,4,5,6,7,8,9,10]; const grouped = Object.groupBy(numbers, n => n % 2 === 0 ? 'even' : 'odd'); console.log(grouped); // { even:[2,4,6,8,10], odd:[1,3,5,7,9] } 3. Mengelompokkan Objek Berdasarkan Properti Copy const products = [ {name:'Laptop', category:'Electronics'}, {name:'Shirt', category:'Clothing'}, {name:'Phone', category:'Electronics'} ]; const groupedProducts = Object.groupBy(products, p => p.category); console.log(groupedProducts); // { Clothing:[{…}], Electronics:[{…},{…}] } 4. Grup Berdasarkan Kriteria Kompleks Copy const numbers = [1,5,10,15,20,25,30]; const groupedByRange = Object.groupBy(numbers, n => n <=10 ? '1-10' : n <=20 ? '11-20' : '21-30'); console.log(groupedByRange); 5. Menangani Nilai Kosong atau Tidak Valid Copy const items = [null,'apple',undefined,'banana','']; const groupedItems = Object.groupBy(items, item => item || 'unknown'); console.log(groupedItems); // { apple:['apple'], banana:['banana'], unknown:[null,undefined,''] } 6. Tips & Catatan Simpel & fleksibel: Bisa grup berdasarkan apa saja. Tidak mengubah array asli → aman untuk data asli. Perlu cek kompatibilitas browser → gunakan polyfill jika perlu. Kesimpulan Pada akhirnya, Object.groupBy() bikin pengelompokan data lebih gampang dan readable. Dari angka sampai objek kompleks, tinggal tentukan key, semua otomatis rapi. Gini deh, kadang hal kecil kayak ini bikin manipulasi data jauh lebih efisien.
chevron-up