Vue Js check if two arrays are equal
Determining Array Equality in Vue.js Using Comparison Techniques : Determining equality between two arrays in Vue.js can be accomplished with the help of JavaScript's built-in functions. One popular technique is to convert both arrays into strings using "JSON.stringify()" and then compare the resulting strings. If they match, the arrays are considered equal.

written

reviewed

updated

Thanks for your feedback!
Your contributions will help us to improve service.
What are the common techniques for checking if two arrays are equal in Vue.js?
- Convert both arrays to strings using
JSON.stringify()
. - Compare the resulting strings using the equality operator (
===
). - If the strings are equal, the arrays are considered equal.
Comparing Array Equality in Vue.js using JSON.stringify()
Copied to Clipboard
xxxxxxxxxx
<div id="app">
<p>Result: {{result}}</p>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
arr1: ['a', 'b', 'c'],
arr2: ['c', 'a', 'b'],
result: null
}
},
mounted() {
if (JSON.stringify(this.arr1.sort()) === JSON.stringify(this.arr2.sort())) {
this.result = 'Array are equal'
} else {
this.result = "Arrays are not equal";
}
}
});
</script>
Output of above example
Ad