Answers for "how to check if 2 arrays have same elements in javascript"

2

check if 2 arrays are equal javascript

const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [1, 2, 3];

function arrayEquals(a, b) {
  return Array.isArray(a) &&
    Array.isArray(b) &&
    a.length === b.length &&
    a.every((val, index) => val === b[index]);
}

arrayEquals(a, b); // false
arrayEquals(a, c); // true
Posted by: Guest on February-04-2021
0

javascript get elements that exist in two arrays

function getArraysIntersection(a1,a2){
    return  a1.filter(function(n) { return a2.indexOf(n) !== -1;});
}
var colors1 = ["red","blue","green"];
var colors2 = ["red","yellow","blue"];
var intersectingColors=getArraysIntersection(colors1, colors2); //["red", "blue"]
Posted by: Guest on August-01-2019
0

check if 2 arrays are equal javascript

var arraysMatch = function (arr1, arr2) {

	// Check if the arrays are the same length
	if (arr1.length !== arr2.length) return false;

	// Check if all items exist and are in the same order
	for (var i = 0; i < arr1.length; i++) {
		if (arr1[i] !== arr2[i]) return false;
	}

	// Otherwise, return true
	return true;

};
Posted by: Guest on December-07-2020

Code answers related to "how to check if 2 arrays have same elements in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language