4 ways to check if arrays are equal

const oldTags = [“facebook”, “twitter”, “instagram”, “dev.to”];
const newTags = [“dev.to”, “twitter”, “facebook”, “instagram”];

// Method 1: Using .sort() & .join()

const arr1 = oldTags.sort().join();
const arr2 = newTags.sort().join();

console…


This content originally appeared on DEV Community and was authored by Mehul Lakhanpal

const oldTags = ["facebook", "twitter", "instagram", "dev.to"];
const newTags = ["dev.to", "twitter", "facebook", "instagram"];

// Method 1: Using .sort() & .join()

const arr1 = oldTags.sort().join();
const arr2 = newTags.sort().join();

console.log("isEqual?", arr1 === arr2);

// Method 2: Using .includes()

let arr1Status = true;
oldTags.forEach((value) => {
  arr1Status = arr1Status && newTags.includes(value);
});

let arr2Status = true;
newTags.forEach((value) => {
  arr2Status = arr2Status && oldTags.includes(value);
});

console.log("isEqual?", arr1Status && arr2Status);

// Method 3: Using .reduce()

let arr1State = oldTags.reduce(
  (acc, value) => acc && newTags.includes(value),
  true
);
let arr2State = newTags.reduce(
  (acc, value) => acc && oldTags.includes(value),
  true
);

console.log("isEqual?", arr1State && arr2State);

// Method 4: Using .isEqual() from Lodash

console.log("isEqual?", _.isEqual(oldTags.sort(), newTags.sort()));

Thanks for reading ?

Follow @codedrops.tech for more.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Projects

Note Box - A chrome extension to add notes/todos based on URL

File Ops - A VS Code extension to easily tag/alias files & quick switch between files


This content originally appeared on DEV Community and was authored by Mehul Lakhanpal


Print Share Comment Cite Upload Translate
APA
Mehul Lakhanpal | Sciencx (2022-09-26T18:16:31+00:00) » 4 ways to check if arrays are equal. Retrieved from https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/.
MLA
" » 4 ways to check if arrays are equal." Mehul Lakhanpal | Sciencx - Thursday April 29, 2021, https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/
HARVARD
Mehul Lakhanpal | Sciencx Thursday April 29, 2021 » 4 ways to check if arrays are equal., viewed 2022-09-26T18:16:31+00:00,<https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/>
VANCOUVER
Mehul Lakhanpal | Sciencx - » 4 ways to check if arrays are equal. [Internet]. [Accessed 2022-09-26T18:16:31+00:00]. Available from: https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/
CHICAGO
" » 4 ways to check if arrays are equal." Mehul Lakhanpal | Sciencx - Accessed 2022-09-26T18:16:31+00:00. https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/
IEEE
" » 4 ways to check if arrays are equal." Mehul Lakhanpal | Sciencx [Online]. Available: https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/. [Accessed: 2022-09-26T18:16:31+00:00]
rf:citation
» 4 ways to check if arrays are equal | Mehul Lakhanpal | Sciencx | https://www.scien.cx/2021/04/29/4-ways-to-check-if-arrays-are-equal/ | 2022-09-26T18:16:31+00:00
https://github.com/addpipe/simple-recorderjs-demo