Bạn có thể so sánh hai mảng trong JavaScript không?

Hãy bắt đầu với phương pháp đơn giản nhất, đó là sử dụng toán tử JSON.stringify()=== để kiểm tra xem các phiên bản được xâu chuỗi của các mảng có bằng nhau không

Đây là một ví dụ

// Define the arrays to compare
const actionMovies = ["Die Hard", "Lethal Weapon", "Mission Impossible"];
const comedyMovies = ["Dumb and Dumber", "The Hangover", "Anchorman"];

// Check if the arrays are equal
if (JSON.stringify(actionMovies) === JSON.stringify(comedyMovies)) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

Trong ví dụ trên, phương thức JSON.stringify() được sử dụng để chuyển đổi mảng thành chuỗi và toán tử === được sử dụng để so sánh các chuỗi. Nếu các mảng bằng nhau, câu lệnh if sẽ in "Các mảng bằng nhau" ra bàn điều khiển, nếu không, nó sẽ in "Các mảng không bằng nhau"

Mảng. nguyên mẫu. mỗi()

Ngoài ra, bạn có thể sử dụng phương thức Array.prototype.every() để so sánh các phần tử của hai mảng. Phương thức

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
0 trả về
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
1 nếu mọi phần tử trong mảng thỏa mãn chức năng kiểm tra được cung cấp và
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
2 nếu không

Đây là một ví dụ

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal

Trong ví dụ trên, phương thức

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
0 được gọi trên mảng
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
4 và nó được truyền một hàm gọi lại để so sánh từng phần tử của mảng
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
4 với phần tử tương ứng của mảng
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
6

Cách tốt hơn một chút để làm điều này là sử dụng

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
7 cùng với
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
8, như vậy

// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal

Sử dụng thuộc tính

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
8 trong ngữ cảnh này sẽ cải thiện hiệu quả và độ chính xác của phép so sánh. Bằng cách kiểm tra độ dài của các mảng trước, mã có thể nhanh chóng xác định xem các mảng có bằng nhau hay không mà không cần so sánh từng phần tử

Điều này tiết kiệm thời gian và tài nguyên, đặc biệt nếu các mảng lớn. Nó cũng tránh được các lỗi tiềm ẩn, chẳng hạn như truy cập một phần tử trong mảng thứ hai không tồn tại, điều này có thể khiến phương thức

// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
0 trả về kết quả không chính xác

Bằng cách sử dụng thuộc tính

// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
8, mã cũng có thể xử lý các trường hợp trong đó các mảng không có độ dài bằng nhau, điều này không thể thực hiện được chỉ với phương thức
// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
0

Mảng. nguyên mẫu. bộ lọc() & Đặt

Các ví dụ trên so sánh các mảng theo thứ tự, nhưng có thể có trường hợp bạn muốn so sánh các mảng bất kể thứ tự. Thay vào đó, bạn chỉ cần xác minh rằng các giá trị giống hệt nhau có trong mỗi mảng

Trong trường hợp đó, bạn có thể sử dụng

// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
3 cùng với
// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
4

// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];

// Create sets from the arrays
const [set1, set2] = [new Set(array1), new Set(array2)];

// Find common elements
const common = [...set1].filter(x => set2.has(x));

// Check if the number of occurrences is the same in both arrays
let equal = true;
common.forEach(value => {
if (array1.filter(x => x === value).length !== array2.filter(x => x === value).length) {
equal = false;
}
});

// Output: true
console.log(equal);
console.log(common);

Ví dụ này kiểm tra xem số lần xuất hiện của các phần tử chung có giống nhau trong cả hai mảng không. Để làm điều này, chúng tôi sử dụng vòng lặp

// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
5 để lặp lại các phần tử phổ biến và so sánh số lượng cho từng phần tử. Nếu tất cả các số đếm bằng nhau, mã sẽ trả về biến
// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
6 là
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
1. Nếu bất kỳ số lượng nào không bằng nhau, mã sẽ đặt biến
// Define the arrays to compare
const array1 = [1, 2, 3, 4, 5];
const array2 = [1, 2, 3, 4];

// Check if the arrays are equal
if (array1.length !== array2.length) {
console.log("The arrays are not equal");
} else if (array1.every((element, index) => element === array2[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
6 thành
// Define the arrays to compare
const primaryColors = ["red", "yellow", "blue"];
const secondaryColors = ["orange", "green", "purple"];

// Check if the arrays are equal
if (primaryColors.every((element, index) => element === secondaryColors[index])) {
console.log("The arrays are equal");
} else {
console.log("The arrays are not equal");
}

// Output: The arrays are not equal
2

Cuối cùng, với ví dụ này, chúng ta cũng log giá trị của biến bằng và mảng chung ra console, để bạn có thể xem kết quả so sánh

Làm cách nào để so sánh hai mảng bằng nhau trong JavaScript?

Để kết luận, để so sánh các mảng nhằm kiểm tra sự bằng nhau, Hàm isEqual() của Lodash là cách tốt nhất nếu bạn cần tất cả các chuông . JSON. phương pháp stringify() hoạt động tốt cho POJO, chỉ cần đảm bảo rằng bạn tính đến null.

Làm thế nào để so sánh 2 mảng?

Làm cách nào để so sánh hai mảng trong Java? .
Chúng cùng loại
Chúng có số phần tử bằng nhau
Các cặp phần tử tương ứng trong cả hai mảng phải bằng nhau
Thứ tự các phần tử phải giống nhau
Hai tham chiếu mảng bằng nhau nếu chúng rỗng

Tại sao JavaScript không thể so sánh các mảng?

Điều đó thật tuyệt. Nhưng thật không may, điều đó không thể thực hiện được vì mảng thực sự là đối tượng trong javascript . Các loại nguyên thủy có thể được so sánh bằng giá trị của chúng trong javascript, như chuỗi và số nguyên. Tuy nhiên, các đối tượng là các loại phức tạp hơn và được so sánh theo tham chiếu của chúng hoặc vị trí chúng được lưu trữ trong bộ nhớ.

Làm cách nào để so sánh hai mảng chuỗi trong js?

Mặc dù JavaScript không có phương thức sẵn có để so sánh trực tiếp hai mảng, nhưng nó lại có phương thức sẵn có để so sánh hai chuỗi. Các chuỗi cũng có thể được so sánh bằng toán tử đẳng thức. Do đó, chúng ta có thể chuyển đổi mảng thành chuỗi, sử dụng phương thức Array join(), sau đó kiểm tra xem các chuỗi có bằng nhau không .