Vấn đề phổ biến: includes() không hoạt động với mảng đa chiều. Array.prototype.flat() làm phẳng mảng nhưng hỗ trợ trình duyệt chưa tốt.
Tạo hàm includesMultiDimension() nhận mảng và chuỗi, trả về boolean dựa trên sự hiện diện của chuỗi.
Cách đơn giản nhất: chuyển mảng thành chuỗi JSON rồi dùng includes.
Ví dụ
const names = ['Ram', 'Shyam', 'Laxman', [ 'Jay', 'Jessica', [ 'Vikram' ] ]]; const includesMultiDimension = (arr, str) => JSON.stringify(arr).includes(str); console.log(includesMultiDimension(names, 'Vikram'));
Kết quả
Đầu ra console:
True