An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
April 29, 2021 05:24 pm GMT
Original Link: https://dev.to/318097/4-ways-to-check-if-arrays-are-equal-4dil
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.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 Lodashconsole.log("isEqual?", _.isEqual(oldTags.sort(), newTags.sort()));
Thanks for reading
Follow @codedrops.tech for more.
Instagram Twitter Facebook
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
Original Link: https://dev.to/318097/4-ways-to-check-if-arrays-are-equal-4dil
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To