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

JavaScript: Mengambil Index Terakhir dengan `findLastIndex()`

1 min read .
JavaScript: Mengambil Index Terakhir dengan `findLastIndex()`

Kadang saya perlu tahu posisi elemen terakhir yang memenuhi kondisi tertentu di array. findLastIndex() bikin ini gampang, tanpa harus reverse array dulu.

1. Dasar findLastIndex()

array.findLastIndex(callback(element[, index[, array]])[, thisArg])
  • callback → fungsi dijalankan di tiap elemen sampai cocok.
  • thisArg → opsional, konteks this.
  • Return → index elemen terakhir yang cocok, atau -1.

2. Contoh Dasar

const numbers = [1,3,5,8,10,12,7,6];
const lastEvenIndex = numbers.findLastIndex(num => num % 2 === 0);
console.log(lastEvenIndex); // 7

3. Contoh Real-World

Transaksi terakhir > 200:

const transactions = [
  {id:1, amount:100},{id:2, amount:200},
  {id:3, amount:300},{id:4, amount:150},{id:5, amount:250}
];

const lastLargeIndex = transactions.findLastIndex(tx => tx.amount > 200);
console.log(lastLargeIndex); // 4

Cari index terakhir ‘Bob’:

const names = ['Alice','Bob','Charlie','Bob','David'];
console.log(names.findLastIndex(name => name === 'Bob')); // 3

4. Catatan

  • Mirip findIndex(), tapi dari belakang → cocok untuk data terbaru.
  • Jika tidak ada match → -1.
  • Praktis untuk log, aktivitas user, atau transaksi terakhir.

Kesimpulan

findLastIndex() bikin kode lebih clean dan efisien saat butuh posisi elemen terakhir yang sesuai kondisi. Gini deh, fitur kecil tapi bikin hidup developer lebih mudah.

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