Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tìm khớp giữa hai phần tử trong JavaScript

Viết hàm trả về true nếu chuỗi ở phần tử đầu tiên chứa tất cả ký tự của chuỗi ở phần tử thứ hai (bỏ qua chữ hoa/thường).

Ví dụ: ['hello', 'Hello'] → true; ['hello', 'hey'] → false (thiếu 'y'); ['Alien', 'line'] → true.

Ví dụ

const arrayContains = ([first, second]) => {  return second  .toLowerCase()  .split('')  .every(char => first.toLowerCase().includes(char));};console.log(arrayContains(['hello', 'HELLO']));console.log(arrayContains(['hello', 'hey']));console.log(arrayContains(['Alien', 'line']));

Kết quả

Đầu ra trong console sẽ là:

true false true