“Cara membandingkan array di JS” Kode Jawaban

JavaScript membandingkan array

Array.prototype.equals = function(arr2) {
  return (
    this.length === arr2.length &&
    this.every((value, index) => value === arr2[index])
  );
};

[1, 2, 3].equals([1, 2, 3]);	// true
[1, 2, 3].equals([3, 6, 4, 2]);	// false
garzj

Cara membandingkan array di JS

// THE PROBLEM:
const firstArray = ["cookies", "milk", "chocolate"]
const secondArray = ["cookies", "milk", "chocolate"]
console.log(firstArray == secondArray) // always returns FALSE

// THE SOLUTION:
if(JSON.stringify(firstArray) === JSON.stringify(secondArray)){
  console.log("firstArray is the same as the secondArray")
} else {
  console.log("firstArray is different from the secondArray")
}
Disgusted Dunlin

Bandingkan dua array dalam JavaScript

let arr1 = [1, 4, 7, 4, 2, 3];
let arr2 = [1, 2, 3, 4, 7, 18];

const is_same = arr1.length == arr2.length &&
  (arr1.every((currElem)=>{
    if(arr2.indexOf(currElem)> -1){
      return (currElem == arr2[arr2.indexOf(currElem)]);
    }return false
  })
)
console.log(is_same)
Gentle Gerenuk

membandingkan dua array dalam javascript

const arr1 = [1, 2, 3];
const arr2 = [1, 3, 3];

if (arr1.length !== arr2.length) return console.log("false");
for (let i = 0; i < arr1.length; i++) {
    for (let j = 0; j < arr2.length; j++) {
        if (arr1[i] === arr2[j]) {
            console.log("yes match", arr1[i], arr2[j]);
            continue;
        }
        console.log("no match", arr1[i], arr2[j]);
    }
}
abhi

Jawaban yang mirip dengan “Cara membandingkan array di JS”

Pertanyaan yang mirip dengan “Cara membandingkan array di JS”

Lebih banyak jawaban terkait untuk “Cara membandingkan array di JS” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya