Πρόγραμμα JavaScript για σύγκριση στοιχείων δύο συστοιχιών

Σε αυτό το παράδειγμα, θα μάθετε να γράφετε ένα πρόγραμμα JavaScript που θα συγκρίνει τα στοιχεία δύο πινάκων.

Για να κατανοήσετε αυτό το παράδειγμα, θα πρέπει να γνωρίζετε τις ακόλουθες ενότητες προγραμματισμού JavaScript:

  • JavaScript για βρόχο
  • Πίνακας JavaScript
  • JavaScript Function and Function Expressions

Παράδειγμα 1: Σύγκριση συστοιχιών χρησιμοποιώντας το JSON.stringify ()

 // program to compare two arrays function compareArrays(arr1, arr2) ( // compare arrays const result = JSON.stringify(arr1) == JSON.stringify(arr2) // if result is true if(result) ( console.log('The arrays have the same elements.'); ) else ( console.log('The arrays have different elements.'); ) ) const array1 = (1, 3, 5, 8); const array2 = (1, 3, 5, 8); compareArrays(array1, array2);

Παραγωγή

 Οι πίνακες έχουν τα ίδια στοιχεία.

Η JSON.stringify()μέθοδος μετατρέπει έναν πίνακα σε συμβολοσειρά JSON.

 JSON.stringify((1, 3, 5, 8)); // "(1,3,5,8)"

Στη συνέχεια, συγκρίνονται οι δύο συμβολοσειρές με τη χρήση ==.

Παράδειγμα 2: Συγκρίνετε πίνακες που χρησιμοποιούν το Loop

 // program to extract value as an array from an array of objects function compareArrays(arr1, arr2) ( // check the length if(arr1.length != arr2.length) ( return false; ) else ( let result = false; // comparing each element of array for(let i=0; i 

Output

 The arrays have the same elements.

In the above program,

The length of the array elements are compared using the length property. If both arrays have different lengths, false is returned.

Else,

  • The for loop is used to iterate through all the elements of the first array.
  • During each iteration, elements of the first array are compared to corresponding elements of the second array.
     arr1(i) != arr2(i)
  • If the corresponding array elements of both arrays are not equal, false is returned and the loop terminates.
  • If all elements are equal, true is returned.

Note: The above program does not work if the array element contains objects.

For example,

 array1 = (1, (a : 2), 3, 5);

ενδιαφέροντα άρθρα...